【发布时间】:2015-01-29 09:58:54
【问题描述】:
请帮我将时间从 12 小时转换为 24 小时格式返回时间在 JAVA 中应该是 UTC
【问题讨论】:
请帮我将时间从 12 小时转换为 24 小时格式返回时间在 JAVA 中应该是 UTC
【问题讨论】:
这将帮助您将时间转换为 24 小时制
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;
}
稍后您可以根据自己的意愿转换为 UTC,或者您可以在一个函数中编译代码
public static String CalculateUTC_Time(String Date, String Time) {
String X[] = Time.split(":");
int Hours = Integer.parseInt(X[0]);
int Minutes = Integer.parseInt(X[1]);
LocalDate date = new LocalDate(Date);
LocalTime time = new LocalTime(Hours, Minutes);
DateTime dt = date.toDateTime(time);
SimpleDateFormat f2 = new SimpleDateFormat("HH:mm");
f2.setTimeZone(TimeZone.getTimeZone("UTC"));
return (f2.format(dt.toDate()));
}
【讨论】: