java.util.Calendar 的默认时区是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20471185/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:11:36  来源:igfitidea点击:

What is the default timezone for java.util.Calendar.?

javadatecalendar

提问by Dileep

Code

代码

public String testDate(){ 
      TimeZone.setDefault(TimeZone.getTimeZone("US/Eastern"));
      Calendar fromDate = Calendar.getInstance();
      Date date= new Date();
      System.out.println(fromDate);
      System.out.println(date);
}

My calendar variable shows a cdate value 2013-12-09T00:00:00.000Zand time value 1386649779590while debugging the calendar variable below.

我的日历变量在调试下面的日历变量时显示 cdate 值2013-12-09T00:00:00.000Z和时间值1386649779590

Calendar cal = Calendar.getInstance();

Complete Calendar details which i have seen while printing the object

我在打印对象时看到的完整日历详细信息

System.out.println(cal);

Console

安慰

java.util.GregorianCalendar[time=1386649779590,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="US/Eastern",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=US/Eastern,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2013,MONTH=11,WEEK_OF_YEAR=50,WEEK_OF_MONTH=2,DAY_OF_MONTH=9,DAY_OF_YEAR=343,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=29,SECOND=39,MILLISECOND=590,ZONE_OFFSET=-18000000,DST_OFFSET=0]

While my java.util.date variable shows a date as Mon Dec 09 07:37:50 EST 2013, while debugging the date variable

虽然我的 java.util.date 变量将日期显示为Mon Dec 09 07:37:50 EST 2013,但在调试日期变量时

Date date= new Date();

where as my default timezonethat i have set is ESTspecified on program start

我设置的默认值timezone是在程序启动指定的EST

TimeZone.setDefault(TimeZone.getTimeZone("US/Eastern"));

And i am working from a timezoneIST.

我在timezoneIST工作。

My question is

我的问题是

Why is calof Calendarand dateof Date()different ?

为什么calofCalendardateofDate()不同?

采纳答案by dbw

According to Oracle Documentationit is clearly mentioned that,

根据Oracle 文档,它清楚地提到,

public static Calendar getInstance()
Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.

public static Calendar getInstance()
使用默认时区和区域设置获取日历。返回的日历基于默认时区和默认语言环境中的当前时间。

And the default time zoneis got by public static TimeZone getDefault()and it is mentioned in TimeZone.getDefault()that

并且default time zone已经得到public static TimeZone getDefault()并且在TimeZone.getDefault() 中提到

Gets the default TimeZone for this host. The source of the default TimeZone may vary with implementation.

获取此主机的默认时区。默认 TimeZone 的来源可能因实现而异。

It will return the defaulttimezone set in your computer until and unless you have used public static void setDefault(TimeZone zone)function to set the TimeZoneexplicitly.

它将返回default在您的计算机中设置的时区,直到并且除非您使用public static void setDefault(TimeZone zone)函数来TimeZone明确设置。

I believe the above explanation answers your both the question,

我相信上面的解释回答了你的两个问题,

  1. What is the default timezone of java.util.Calendar.?
  2. Why is my variable cal of type Calendar shows a time that is not IST or EST.?
  1. java.util.Calendar 的默认时区是什么?
  2. 为什么我的日历类型的变量 cal 显示的时间不是 IST 或 EST。?

EDIT:As per your edited question

编辑:根据您编辑的问题

Why is cal of Calendar and date of Date() different?

为什么 Calendar 的 cal 和 Date() 的日期不同?

When you call System.out.println(date);then toString()function is invoked and if you look at Source Code of Dateyou will find that it returns 3 letters shorthand for timezone by invoking the displayNamefunction of default time zone which is 3 letters shorthand in your case EST, which is U.S. Eastern Standard Time (GMT-05:00) Indiana (East).

当你调用System.out.println(date);thentoString()函数被调用,如果你查看日期的源代码,你会发现它通过调用displayName默认时区的函数返回 3 个字母的时区简写,在你的情况下是 3 个字母的简写EST,即U.S. Eastern Standard Time (GMT-05:00) Indiana (East).

回答by Lingasamy Sakthivel

The date itself doesn't have any time zone. It's toString()method uses the current default time zone to return a String

日期本身没有任何时区。它的toString()方法使用当前默认时区返回一个字符串

回答by Abimaran Kugathasan

Typically, you get a TimeZoneusing getDefaultwhich creates a TimeZonebased on the time zone where the program is running. For example, for a program running in Japan, getDefaultcreates a TimeZoneobject based on Japanese Standard Time.

通常,您会得到一个TimeZoneusing getDefault,它TimeZone根据程序运行的时区创建一个。例如,对于在日本运行的程序,getDefault创建TimeZone基于日本标准时间的对象。

Check TimeZonefor more

检查TimeZone更多

回答by dcsohl

You mention "cdate", and I notice there is a field inside the Calendar object called cdate. In running your sample code, I see that the cdatefield is indeed initialized to 2013-12-10T00:00:00.000Z(it now being 24 hours later of course).

您提到了“cdate”,我注意到 Calendar 对象中有一个名为cdate. 在运行您的示例代码时,我看到该cdate字段确实已初始化为2013-12-10T00:00:00.000Z(当然现在是 24 小时后)。

So? I don't know why you are looking at internal fields of a class when you are never going to directly use them.

所以?我不知道为什么当您永远不会直接使用它们时,您要查看类的内部字段。

Your solution, then, is to ignore it. Don't worry about the cdatefield of your Calendar; worry about things that actually affect your program.

那么,您的解决方案是忽略它。不用担心cdate日历的领域;担心实际影响您的程序的事情。

The toString()of a Calendaris not very pretty and is intended for debugging; you should call cal.getTime()which will give you a java.util.Datethat you can then print out either directly or by using a java.text.DateFormatter.

toString()一个Calendar不是很漂亮,并用于调试; 您应该调用cal.getTime()它会给您一个java.util.Date,然后您可以直接或使用java.text.DateFormatter.