In this post we’ll see how to convert 24 hours time format to 12 hours time format and 12 hours time format to 24 hours time format in Java.
For the conversion you can either use SimpleDateFormat or Java 8 onward you can use DateTimeFormatter class to do the conversion from 24 hour time to 12 hour time or vice versa in Java.Table of contents
- Convert 24 hours time format to 12 hours time format using SimpleDateFormat
- Convert 12 hours time format to 24 hours time format using SimpleDateFormat
- Convert 24 hours time format to 12 hours time format using DateTimeFormatter
- Convert 12 hours time format to 24 hours time format using DateTimeFormatter
Convert 24 hours time format to 12 hours time format using SimpleDateFormat
Just remember that for 24 hours format you have to use HH where as for 12 hours time you will have to use hh. Based on that you create two SimpleDateFormat instances by passing the appropriate patterns.
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TimeConversion { public static void main(String[] args) { String fromPattern = "HH:mm:ss"; // 24 hour time pattern String toPattern = "hh:mm:ss a"; // 12 hour time pattern with AM/PM String time = "21:15:34"; convertUsingPattern(time, fromPattern, toPattern); } public static void convertUsingPattern(String time, String convertFromPattern, String convertToPattern){ DateFormat dfFrom = new SimpleDateFormat(convertFromPattern); DateFormat dfTo = new SimpleDateFormat(convertToPattern); try { Date date = dfFrom.parse(time); System.out.println("From Time- " + date); String changedTime = dfTo.format(date); System.out.println("To Time- " + changedTime); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }Output
From Time- Thu Jan 01 21:15:34 IST 1970 To Time- 09:15:34 PM
Convert 12 hours time format to 24 hours time format using SimpleDateFormat
In the above program just interchange the fromPattern and toPattern.
public class TimeConversion { public static void main(String[] args) { String fromPattern = "hh:mm:ss a"; // 12 hour time pattern with AM/PM String toPattern = "HH:mm:ss"; // 24 hour time pattern String time = "09:15:34 AM"; convertUsingPattern(time, fromPattern, toPattern); } public static void convertUsingPattern(String time, String convertFromPattern, String convertToPattern){ DateFormat dfFrom = new SimpleDateFormat(convertFromPattern); DateFormat dfTo = new SimpleDateFormat(convertToPattern); try { Date date = dfFrom.parse(time); System.out.println("From Time- " + date); String changedTime = dfTo.format(date); System.out.println("To Time- " + changedTime); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }Output
From Time- Thu Jan 01 09:15:34 IST 1970 To Time- 09:15:34 If input is- 09:15:34 PM then output From Time- Thu Jan 01 21:15:34 IST 1970 To Time- 21:15:34
Convert 24 hours time format to 12 hours time format using DateTimeFormatter
import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; public class TimeConversion { public static void main(String[] args) { String fromPattern = "HH:mm:ss"; // 24 hour time pattern String toPattern = "hh:mm:ss a"; // 12 hour time pattern with AM/PM String time = "14:34:45"; convertUsingPattern(time, fromPattern, toPattern); } public static void convertUsingPattern(String time, String convertFromPattern, String convertToPattern){ try { LocalTime lt = LocalTime.parse(time, DateTimeFormatter.ofPattern(convertFromPattern)); System.out.println("From Time- " + lt.toString()); String changedTime = lt.format(DateTimeFormatter.ofPattern(convertToPattern)); System.out.println("To Time- " + changedTime); } catch (DateTimeParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }Output
From Time- 14:34:45 To Time- 02:34:45 PM
Convert 12 hours time format to 24 hours time format using DateTimeFormatter
In the above program just interchange the fromPattern and toPattern.
public class TimeConversion { public static void main(String[] args) { String fromPattern = "hh:mm:ss a"; // 12 hour time pattern with AM/PM String toPattern = "HH:mm:ss"; // 24 hour time pattern String time = "12:34:45 AM"; convertUsingPattern(time, fromPattern, toPattern); } public static void convertUsingPattern(String time, String convertFromPattern, String convertToPattern){ try { LocalTime lt = LocalTime.parse(time, DateTimeFormatter.ofPattern(convertFromPattern)); System.out.println("From Time- " + lt.format(DateTimeFormatter.ofPattern(convertFromPattern))); String changedTime = lt.format(DateTimeFormatter.ofPattern(convertToPattern)); System.out.println("To Time- " + changedTime); } catch (DateTimeParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }Output
From Time- 12:34:45 AM To Time- 00:34:45
That's all for the topic Convert 24 Hours Time to 12 Hours Time Format in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment