在UTC范围内,用JAVA将12小时转换为24小时
请帮助我将时间从12小时转换为24小时格式返回时间应以JAVA中的UTC为单位这将帮助您将时间转换为24小时在UTC范围内,用JAVA将12小时转换为24小时,java,time,utc,Java,Time,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 (ParseExce
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()));
}