Java 使用日历和日期获取 UTC 时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24826616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting UTC time with Calendar and Date
提问by Lucas Jota
I'm trying to get an instance of Date with UTC time using the following code:
我正在尝试使用以下代码获取具有 UTC 时间的 Date 实例:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date now = cal.getTime();
that looks so simple, but if I check the values at IntelliJ's debugger, I get different dates for cal
and now
:
这看起来很简单,但是如果我在 IntelliJ 的调试器中检查值,我会得到不同的日期cal
和now
:
cal
:java.util.GregorianCalendar[time=1405690214219,areFieldsSet=true,lenient=true,zone=GMT,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=29,WEEK_OF_MONTH=3,DAY_OF_MONTH=18,DAY_OF_YEAR=199,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=30,SECOND=14,MILLISECOND=219,ZONE_OFFSET=0,DST_OFFSET=0]
cal
:java.util.GregorianCalendar[time=1405690214219,areFieldsSet=true,lenient=true,zone=GMT,firstDayOfWeek=2,minimalDaysInFirstWeek=4,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=29,WEEK3OF_OF_ ,DAY_OF_MONTH=18,DAY_OF_YEAR=199,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=1, HOUR_OF_DAY=13,MINUTE=30,SECOND=14,MILLISECOND=219,ZONE_OFFSET
now
:Fri Jul 18 10:30:14 BRT 2014
now
:2014 年 7 月 18 日星期五 10:30:14 BRT
as you can see, cal
is 3 hours ahead of now
... what am I doing wrong?
正如你所看到的,cal
是提前 3 小时now
......我做错了什么?
Thanks in advance.
提前致谢。
[EDIT] Looks like TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
before the code above does the job...
[编辑] 看起来TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
在上面的代码完成工作之前......
回答by dsum27
This question has already been answered here
The System.out.println(cal_Two.getTime()) invocation returns a Date from getTime(). It is the Date which is getting converted to a string for println, and that conversion will use the default IST timezone in your case.
System.out.println(cal_Two.getTime()) 调用从 getTime() 返回一个日期。它是转换为 println 字符串的日期,并且在您的情况下,该转换将使用默认的 IST 时区。
You'll need to explicitly use DateFormat.setTimeZone() to print the Date in the desired timezone.
您需要明确使用 DateFormat.setTimeZone() 在所需时区打印日期。
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("EE MMM dd HH:mm:ss zzz yyyy", Locale.US);
simpleDateFormat.setTimeZone(timeZone);
System.out.println("Time zone: " + timeZone.getID());
System.out.println("default time zone: " + TimeZone.getDefault().getID());
System.out.println();
System.out.println("UTC: " + simpleDateFormat.format(calendar.getTime()));
System.out.println("Default: " + calendar.getTime());
Edit To convert cal to date
编辑将 cal 转换为日期
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
System.out.println(year);
Date date = new Date(year - 1900, month, day); // is same as date = new Date();
Just build the Date object using the Cal values. Please let me know if that helps.
只需使用 Cal 值构建 Date 对象。如果这有帮助,请告诉我。
回答by johns4ta
Try using a date formatter and set the time zone to UTC.
尝试使用日期格式化程序并将时区设置为 UTC。
dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));