日历返回错误的月份和年份Java

时间:2014-10-06 00:25:22

标签: java date calendar

    Calendar now = null;
    now.getInstance();
    System.out.println(now.getInstance());
    System.out.println(now.ERA);
    System.out.println(now.YEAR);
    System.out.println(now.MONTH);
    System.out.println(now.WEEK_OF_YEAR);
    System.out.println(now.WEEK_OF_MONTH);
    System.out.println(now.DAY_OF_MONTH);
    System.out.println(now.DAY_OF_YEAR);
    System.out.println(now.DAY_OF_WEEK);
    System.out.println(now.DAY_OF_WEEK_IN_MONTH);

计划输出(重要信息在大写字母和新行中):

java.util.GregorianCalendar[time=1412554865330,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Los_Angeles",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=America/Los_Angeles,offset=-28800000,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=2014,MONTH=9,WEEK_OF_YEAR=41,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=278,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=5,HOUR_OF_DAY=17,MINUTE=21,SECOND=5,MILLISECOND=330,ZONE_OFFSET=-28800000,DST_OFFSET=3600000]

0
1
2
3
4
5
6
7
8

(0-8在新线上) 我真的在这里失去了,我不确定为什么java决定在我身上玩这个糟糕的恶作剧。

3 个答案:

答案 0 :(得分:3)

这些是static类中的Calendar个字段。 Java allows static fields (and methods) to be accessed on expressions that resolve to an instance's reference value与类型名称相同

now.ERA
// is equivalent to
Calendar.ERA

Calendar类提供get(int)方法来获取日期字段的值。

now.get(Calendar.MONTH);

答案 1 :(得分:0)

约达时间

仅供参考,您可能会发现使用Joda-Time库(或Java 8中的java.time package)比使用java.util.Date& java.util.Calendar类。

Joda-Time和java.time都遵循ISO 8601来定义星期和默认字符串格式。

这是相同类型的代码,但使用Joda-Time 2.4。

DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );
java.util.Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter formatter = DateTimeFormat.forStyle( "FF" ).withLocale( locale );
DateTime dateTime = DateTime.now( timeZone );

转储到控制台。

System.out.println( "Full: " + formatter.print( dateTime ) );
System.out.println( "Era: " + dateTime.getEra() ); // 0 (BC/BCE) & 1 (AD/CE).
System.out.println( "Year: " + dateTime.getYear() );
System.out.println( "Month: " + dateTime.getMonthOfYear() );
System.out.println( "WeekOfYear: " + dateTime.getWeekOfWeekyear() ); // Standard ISO 8601 week.
//System.out.println( now.WEEK_OF_MONTH ); // Not in Joda-Time as there is no standard definition for week-of-month.
System.out.println( "DayOfMonth: " + dateTime.getDayOfMonth() );
System.out.println( "DayOfYear: " + dateTime.getDayOfYear() );
System.out.println( "DayOfWeek: " + dateTime.getDayOfWeek() ); // Starts at 1 rather than 0. Amazing!
//System.out.println( now.DAY_OF_WEEK_IN_MONTH ); // Not in Joda-Time as there is no standard definition for week-of-month.
System.out.println( "TimeZone: " + dateTime.getZone() );

跑步时。

Full: dimanche 5 octobre 2014 21 h 22 EDT
Era: 1
Year: 2014
Month: 10
WeekOfYear: 40
DayOfMonth: 5
DayOfYear: 278
DayOfWeek: 7
TimeZone: America/Montreal

答案 2 :(得分:-1)

如果您可以移植到Java8

,我建议使用Java8的LocalDateTime和LocalTime类

否则我建议使用GregorianCalendar cal = new GregorianCalendar()

cal.get(Calendar.MONTH) for example