Conversion from 12 hours time to 24 hours time in java

后端 未结 14 1068
失恋的感觉
失恋的感觉 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:15

    Try this:

    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Main {
       public static void main(String [] args) throws Exception {
           SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
           SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
           Date date = parseFormat.parse("10:30 PM");
           System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
       }
    }
    

    which produces:

    10:30 PM = 22:30
    

    See: http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

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

    Try This

    public static String convertTo24Hour(String Time) {
        DateFormat f1 = new SimpleDateFormat("hh:mm a"); //11:00 pm
        Date d = null;
        try {
            d = f1.parse(Time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        DateFormat f2 = new SimpleDateFormat("HH:mm");
        String x = f2.format(d); // "23:00"
    
        return x;
    }
    
    0 讨论(0)
  • 2020-11-29 06:22
       static String timeConversion(String s)
       {
        String s1[]=s.split(":");
        char c[]=s1[2].toCharArray();
        if(s1[2].contains("PM"))
        {
            int n=Integer.parseInt(s1[0]);
            n=n+12;
            return n+":"+s1[1]+":"+c[0]+c[1];
        }
        else``
        return s1[0]+":"+s1[1]+":"+c[0]+c[1];
         }
    
    0 讨论(0)
  • 2020-11-29 06:22
    import java.text.SimpleDateFormat;
    import java.text.DateFormat;
    import java.util.Date;
    
    public class Main {
       public static void main(String [] args){
           try {
                DateFormat parseFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
                String sDate = "22-01-2019 9:0:0 PM";
                Date date = parseFormat.parse(sDate);
                SimpleDateFormat displayFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
                sDate = displayFormat.format(date);
                LOGGER.info("The required format : " + sDate);
               } catch (Exception e) {}
          }
    }
    
    0 讨论(0)
  • 2020-11-29 06:26

    java.time

    In Java 8 and later it could be done in one line using class java.time.LocalTime.

    In the formatting pattern, lowercase hh means 12-hour clock while uppercase HH means 24-hour clock.

    Code example:

    String result =                                       // Text representing the value of our date-time object.
        LocalTime.parse(                                  // Class representing a time-of-day value without a date and without a time zone.
            "03:30 PM" ,                                  // Your `String` input text.
            DateTimeFormatter.ofPattern(                  // Define a formatting pattern to match your input text.
                "hh:mm a" ,
                Locale.US                                 // `Locale` determines the human language and cultural norms used in localization. Needed here to translate the `AM` & `PM` value.
            )                                             // Returns a `DateTimeFormatter` object.
        )                                                 // Return a `LocalTime` object.
        .format( DateTimeFormatter.ofPattern("HH:mm") )   // Generate text in a specific format. Returns a `String` object.
    ;
    

    See this code run live at IdeOne.com.

    15:30

    See Oracle Tutorial.

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

    Try this to calculate time difference between two times.

    first it will convert 12 hours time into 24 hours then it will take diff between two times

    String a = "09/06/18 01:55:33 AM";
                String b = "07/06/18 05:45:33 PM";
                String [] b2 = b.split(" ");
                String [] a2 = a.split(" ");
                SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm:ss");
                SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm:ss a");
                String time1 = null ;
                String time2 = null ;
                if ( a.contains("PM") && b.contains("AM")) {
                     
                     Date date = parseFormat.parse(a2[1]+" PM");
                     time1 = displayFormat.format(date);
                     time2 = b2[1];
                }else if (b.contains("PM") && a.contains("AM")) {
                    Date date = parseFormat.parse(a2[1]+" PM");
                    time1 = a2[1];
                    time2 = displayFormat.format(date);
                }else if (a.contains("PM") && b.contains("PM")){
                    Date datea = parseFormat.parse(a2[1]+" PM");
                    Date dateb = parseFormat.parse(b2[1]+" PM");
                    time1 = displayFormat.format(datea);
                    time2 = displayFormat.format(dateb);
                }   
                System.out.println(time1);
                System.out.println(time2);      
                SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
                Date date1 = format.parse(time1);
                Date date2 = format.parse(time2);
                long difference = date2.getTime() - date1.getTime(); 
                System.out.println(difference);
                System.out.println("Duration: "+DurationFormatUtils.formatDuration(difference, "HH:mm"));
    

    For More Details Click Here

    0 讨论(0)
提交回复
热议问题