java.util.Calendar的默认时区是多少? [英] What is the default timezone for java.util.Calendar.?

查看:205
本文介绍了java.util.Calendar的默认时区是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码

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);
}

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

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

Calendar cal = Calendar.getInstance();

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

Complete Calendar details which i have seen while printing the object

System.out.println(cal);

控制台

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]

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

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

Date date= new Date();

我的默认值为$ code> timezone set is EST 在程序启动时指定

where as my default timezone that i have set is EST specified on program start

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

我正在从时区 strong> IST 。

And i am working from a timezone IST.

我的问题是

为什么 cal 日历日期 Date()

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

推荐答案

根据 Oracle文档,显然提到,


public static Calendar getInstance()

使用默认时区和区域设置获取日历。返回的日历是基于默认时区的默认区域设置的当前时间。

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 TimeZone getDefault()。 com / javase / 7 / docs / api / java / util / TimeZone.html#getDefault()rel =noreferrer> TimeZone.getDefault()


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

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

它将返回默认值时区设置在您的计算机中,除非您使用 public static void setDefault(TimeZone zone)函数设置 TimeZone 明确地

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

我相信上述说明可以解答您的问题,


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

  2. 为什么我的日历类型的变量cal显示不是IST或EST的时间

编辑:根据您编辑的问题


为什么日历的日期和日期的日期不同?

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

当你打电话 System.out.println(date); then toString()函数被调用,如果你查看源代码日期,你会发现它通过调用<$ c来返回时区的3个字母的缩写$ c> displayName 默认时区的功能,您的情况下为3个字母的缩写 EST ,即 US东区标准时间(GMT-05:00)印第安纳州(东)

When you call System.out.println(date); then toString() function is invoked and if you look at Source Code of Date you will find that it returns 3 letters shorthand for timezone by invoking the displayName function 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).

这篇关于java.util.Calendar的默认时区是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆