在Java中将24小时制转换为12小时制
时间:2020-01-09 10:35:24 来源:igfitidea点击:
在本文中,我们将介绍如何在Java中将24小时制转换为12小时制,将12小时制转换为24小时制。
对于转换,我们可以使用SimpleDateFormat或者Java 8或者更高版本,可以使用DateTimeFormatter类在24小时内转换为12小时,反之亦然。
使用SimpleDateFormat将24小时制转换为12小时制
请记住,对于24小时格式,我们必须使用HH,而对于12小时时间,则必须使用hh。基于此,我们可以通过传递适当的模式来创建两个SimpleDateFormat实例。
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(); } } }
输出量
From Time- Thu Jan 01 21:15:34 IST 1970 To Time- 09:15:34 PM
使用SimpleDateFormat将12小时制转换为24小时制
在上面的程序中,只需将fromPattern和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(); } } }
输出量
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
使用DateTimeFormatter将24小时格式转换为12小时格式
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(); } } }
输出量
From Time- 14:34:45 To Time- 02:34:45 PM
使用DateTimeFormatter将12小时时间格式转换为24小时时间格式
在上面的程序中,只需将fromPattern和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(); } } }
输出量
From Time- 12:34:45 AM To Time- 00:34:45