Conversion from 12 hours time to 24 hours time in java

后端 未结 14 1069
失恋的感觉
失恋的感觉 2020-11-29 06:08

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

For example, time like 10

相关标签:
14条回答
  • 2020-11-29 06:29

    I was looking for same thing but in number, means from integer xx hour, xx minutes and AM/PM to 24 hour format xx hour and xx minutes, so here what i have done:

    private static final int AM = 0;
    private static final int PM = 1;
    /**
       * Based on concept: day start from 00:00AM and ends at 11:59PM, 
       * afternoon 12 is 12PM, 12:xxAM is basically 00:xxAM
       * @param hour12Format
       * @param amPm
       * @return
       */
      private int get24FormatHour(int hour12Format,int amPm){
        if(hour12Format==12 && amPm==AM){
          hour12Format=0;
        }
        if(amPm == PM && hour12Format!=12){
          hour12Format+=12;
        }
        return hour12Format;
      }`
    
        private int minutesTillMidnight(int hour12Format,int minutes, int amPm){
            int hour24Format=get24FormatHour(hour12Format,amPm);
            System.out.println("24 Format :"+hour24Format+":"+minutes); 
            return (hour24Format*60)+minutes;
          }
    
    0 讨论(0)
  • 2020-11-29 06:30

    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

    0 讨论(0)
  • 2020-11-29 06:32

    12 to 24 hour time conversion and can be reversed if change time formate in output and input SimpleDateFormat class parameter

    Test Data Input:

    String input = "07:05:45PM"; timeCoversion12to24(input);

    output

    19:05:45

     public static String timeCoversion12to24(String twelveHoursTime) throws ParseException {
    
            //Date/time pattern of input date (12 Hours format - hh used for 12 hours)
            DateFormat df = new SimpleDateFormat("hh:mm:ssaa");
    
            //Date/time pattern of desired output date (24 Hours format HH - Used for 24 hours)
            DateFormat outputformat = new SimpleDateFormat("HH:mm:ss");
            Date date = null;
            String output = null;
    
            //Returns Date object
            date = df.parse(twelveHoursTime);
    
            //old date format to new date format
            output = outputformat.format(date);
            System.out.println(output);
    
            return output;
        }
    
    0 讨论(0)
  • 2020-11-29 06:35

    Using LocalTime in Java 8, LocalTime has many useful methods like getHour() or the getMinute() method,

    For example,

    LocalTime intime = LocalTime.parse(inputString, DateTimeFormatter.ofPattern("h:m a"));
    String outtime = intime.format(DateTimeFormatter.ISO_LOCAL_TIME);
    

    In some cases, First line alone can do the required parsing

    0 讨论(0)
  • 2020-11-29 06:38

    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]);
        }
    
    0 讨论(0)
  • 2020-11-29 06:38

    I have written a simple utility function.

    public static String convert24HourTimeTo12Hour(String timeStr) {
        try {
            DateFormat inFormat = new SimpleDateFormat( "HH:mm:ss");
            DateFormat outFormat = new SimpleDateFormat( "hh:mm a");
            Date date = inFormat.parse(timeStr);
            return outFormat.format(date);
        }catch (Exception e){}
    
        return "";
    }
    
    0 讨论(0)
提交回复
热议问题