我正在使用本地时间转换为UTC和Utc到本地,同时选择时间12:00 PM当地时间(悉尼澳大利亚)它在Utc转换时间01:00但它应该是02:00,同时将UTC转换为本地它正确的时间但是日期将是下一天的日期,而选择其他时间它会给出正确的输出
请给出一些解决方案
谢谢
答案 0 :(得分:0)
public static String convertLocalTimeToUTC(String p_city, String p_localDateTime) throws Exception{
String lv_dateFormateInUTC="";//Will hold the final converted date
Datelv_localDate = null;
Stringlv_localTimeZone ="";
SimpleDateFormat lv_formatter;
SimpleDateFormat lv_parser;
//Temp for testing(mapping of cities and timezones will eventually be in a properties file
if(p_city.equals("LON")){
lv_localTimeZone="Europe/London";
}else if(p_city.equals("NBI")){
lv_localTimeZone="EAT";
}else if(p_city.equals("BRS")){
lv_localTimeZone="Europe/Brussels";
}else if(p_city.equals("MNT")){
lv_localTimeZone="America/Montreal";
}else if(p_city.equals("LAS")){
lv_localTimeZone="PST";
}
//create a new Date object using the timezone of the specified city
lv_parser = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
lv_parser.setTimeZone(TimeZone.getTimeZone(lv_localTimeZone));
lv_localDate = lv_parser.parse(p_localDateTime);
//Set output format prints "2007/10/25 18:35:07 EDT(-0400)"
lv_formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z'('Z')'");
lv_formatter.setTimeZone(TimeZone.getTimeZone(lv_localTimeZone));
System.out.println("convertLocalTimeToUTC: "+p_city+": "+" The Date in the local time zone " + lv_formatter.format(lv_localDate));
//Convert the date from the local timezone to UTC timezone
lv_formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
lv_dateFormateInUTC = lv_formatter.format(lv_localDate);
System.out.println("convertLocalTimeToUTC: "+p_city+": "+" The Date in the UTC time zone " + lv_dateFormateInUTC);
return lv_dateFormateInUTC;
}