Conversion from 12 hours time to 24 hours time in

2019-01-07 17:56发布

In my app, i have requirement to format 12 hours time to 24 hours time.What is the method i have to use?

For example, time like 10:30 AM. How can i convert to 24 hours time in java?

标签: java time
9条回答
干净又极端
2楼-- · 2019-01-07 18:54

This is the extract of code that I have done.

    String s="08:10:45";
    String[] s1=s.split(":");
    int milipmHrs=0;
    char[] arr=s1[2].toCharArray();
    boolean isFound=s1[2].contains("PM");
    if(isFound){
        int pmHrs=Integer.parseInt(s1[0]);
        milipmHrs=pmHrs+12;
        return(milipmHrs+":"+s1[1]+":"+arr[0]+arr[1]);
    }
    else{

        return(s1[0]+":"+s1[1]+":"+arr[0]+arr[1]);
    }
查看更多
SAY GOODBYE
3楼-- · 2019-01-07 18:56
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a"); 

provided by Bart Kiers answer should be replaced with somethig like

SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a",Locale.UK);
查看更多
ら.Afraid
4楼-- · 2019-01-07 18:57

Assuming that you use SimpleDateFormat implicitly or explicitly, you need to use H instead of h in the format string.

E.g

HH:mm:ss

instead of

hh:mm:ss

查看更多
登录 后发表回答