【问题标题】:give Wrong date while conversion local time to UTC time android?在将本地时间转换为 UTC 时间 android 时给出错误的日期?
【发布时间】:2015-09-28 15:44:26
【问题描述】:

我正在使用时间转换 Local to UTC 和 Utc to local,同时选择 Time 12:00 PM local time (Sydney Australia) 它转换时间 01:00 in Utc 但它应该是 02:00 ,同时将 UTC 转换为 Local它正确的时间但日期将是第二天的日期,而选择其他时间它会给出正确的输出
请给出一些解决方案 谢谢你

【问题讨论】:

  • 请展示一个简短但完整的程序来说明问题 - 您的描述几乎不够清楚。 (特别是日期也很重要……我们需要完整的输入数据和输出数据。)

标签: android date datetime time


【解决方案1】:
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;
}

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多