将UTC日期转换为当前时区 [英] Convert UTC date to current timezone
问题描述
我必须将格式为"2016-09-25 17:26:12"的UTC日期转换为Android的当前时区.我是这样做的:
I have to convert a UTC date in this format "2016-09-25 17:26:12" to the current time zone of Android. I did this:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(temp.getString("date"));
System.out.println(mydate.toString());
这有效,但我不知道为什么它还会打印"GMT + 02:00".这是输出:"Sun Sep 25 19:26:12 GMT + 02:00 2016",我不想显示"GMT + 02:00".
This works but I don't know why it print also "GMT+02:00". This is the Output: "Sun Sep 25 19:26:12 GMT+02:00 2016", I don't want to show "GMT+02:00".
编辑,解决方案的代码:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat outputSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date myDate = simpleDateFormat.parse(temp.getString("date"));
System.out.println(outputSdf.format(myDate));
推荐答案
它会打印GMT + 02,因为这是您的本地"时区.如果要打印不带时区信息的日期,请使用SimpleDateFormat设置日期格式.
it print GMT+02 because this is your "local" timezone. if you want to print the date without timezone information, use SimpleDateFormat to format the date to you liking.
edit:添加代码示例(带有变量"myDate")
edit : adding the code example (with your variable 'myDate')
SimpleDateFormat inputSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
inputSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = inputSDF.parse("2016-09-25 17:26:12");
//
SimpleDateFormat outputSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(outputSDF.format(myDate));
System.out.println(TimeZone.getDefault().getID());
(我的)控制台上的收益(带有我的本地时区).
yield on the (my) console (with my local timezone).
2016-09-25 19:26:12
Europe/Paris
这篇关于将UTC日期转换为当前时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!