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

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

问题描述

代码

  public String testDate(){
TimeZone.setDefault .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 时调试下面的日历变量。

 日历cal = Calendar.getInstance(); 

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

  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 ,同时调试日期变量

 日期date = new Date(); 

其中作为我的默认 timezone

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

我正在从 timezone strong> IST 。



我的问题是



为什么 cal 日历日期

解决方案

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


public static日历getInstance()

使用默认时区和区域设置获取日历。


而且会根据预设时区的目前时间,默认时区 public static TimeZone getDefault()获得,并且在 TimeZone.getDefault()


获取此主机的默认TimeZone。


它将返回默认值 c> timezone在您的计算机中设置,除非您已使用 public static void setDefault(TimeZone zone)函数设置 TimeZone



我相信上述解释会回答您的问题 ol>

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

  • 为什么我的变量cal类型Calendar显示的时间不是IST或EST 。

  • EDIT:根据您编辑的问题


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


    System.out.println(date); 然后 toString()函数被调用,如果你看看源代码日期,您会发现它通过调用默认时区的 displayName 函数返回3个字母的时区缩写,这是您的案例中的3个字母缩写 EST ,即 US东部标准时间(GMT-05:00)印第安纳州(东)


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

    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

    Date date= new Date();
    

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

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

    And i am working from a timezone IST.

    My question is

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

    解决方案

    According to Oracle Documentation it is clearly mentioned that,

    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.

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

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

    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.

    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.?

    EDIT: As per your edited question

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

    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天全站免登陆