Conversion from 12 hours time to 24 hours time in java

后端 未结 14 1100
失恋的感觉
失恋的感觉 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;
          }
    

提交回复
热议问题