简体   繁体   English

Calendar.Month 给出错误的输出

[英]Calendar.Month gives wrong output

I have been using java.util for all date and calendar representations.我一直在使用java.util来表示所有日期和日历。 But I am facing a strange problem here.但我在这里面临一个奇怪的问题。 Calendar.MONTH , Calendar.DAY_OF_MONTH , etc all give wrong outputs. Calendar.MONTHCalendar.DAY_OF_MONTH等都会给出错误的输出。 But when I use Calendar.getTime() , I get the right output.但是当我使用Calendar.getTime() ,我得到了正确的输出。 What might be the problem?可能是什么问题?

public class TestClass {

    public static void main(String[] args) {
        Calendar rightNow = Calendar.getInstance();
        System.out.println(rightNow.MONTH);
        System.out.println(rightNow.DAY_OF_MONTH);
        System.out.println(rightNow.YEAR);
        System.out.println(rightNow.getTime());
    }
}

And the output for the same is:同样的输出是:

2
5
1
Tue Jan 22 10:31:44 GMT+05:30 2013
    System.out.println(rightNow.MONTH);
    System.out.println(rightNow.DAY_OF_MONTH);
    System.out.println(rightNow.YEAR);
    System.out.println(rightNow.getTime());

You are printing Calendar constant values.您正在打印日历常量值。

If you want values, you need to do get....如果你想要值,你需要做得到get....

Example:例子:

    System.out.println(rightNow.get(Calendar.MONTH));

Read Calendar javadoc for more information.阅读日历 javadoc了解更多信息。

Calendar.MONTH doesn't return the current month. Calendar.MONTH 不返回当前月份。 It is a constant whose value is 2.它是一个常数,其值为 2。

From the source:从来源:

public final static int MONTH = 2;

It is for use as a parameter in Calendar's get method它用作 Calendar 的 get 方法中的参数

public int get(int field) {...}

The design of Calendar is from the early days of Java, when many of today's conventions were different, non-existant, or developing. Calendar 的设计起源于 Java 的早期,当时许多今天的约定都不同、不存在或正在发展。 Oracle (and earlier, Sun), would never let an API like the java.util.Calendar API become part of the standard API today. Oracle(以及更早的 Sun)永远不会让像 java.util.Calendar API 这样的 API 成为当今标准 API 的一部分。

And, for completeness, use Joda Time instead of Java's Date and Calendar API's.并且,为了完整起见,请使用Joda Time而不是 Java 的日期和日历 API。

java.time时间

    ZonedDateTime rightNow = ZonedDateTime.now(ZoneId.of("Africa/Cairo"));
    System.out.println(rightNow.getMonth());
    System.out.println(rightNow.getMonthValue());
    System.out.println(rightNow.getDayOfMonth());
    System.out.println(rightNow.getYear());
    System.out.println(rightNow);

Output when running just now was:刚才运行时的输出是:

 FEBRUARY 2 15 2019 2019-02-15T21:15:06.313809+02:00[Africa/Cairo]

Don't use Calendar不要使用日历

The Calendar class is confusing and suffers from poor design, so no wonder that you're wondering. Calendar类令人困惑并且设计不佳,所以难怪您想知道。 Fortunately it was replaced the year after you asked this quesion by ZonedDateTime and other classes in java.time, the modern Java date and time API.幸运的是,在您提出这个问题的ZonedDateTime ,它被ZonedDateTime和 java.time(现代 Java 日期和时间 API)中的其他类所取代。 So never use Calendar again now.所以现在不要再使用Calendar了。

The Calendar class was trying to be very general, so instead of a getMonth method, etc., it had the notion of fields that could be read and set. Calendar类试图变得非常通用,因此它具有可以读取和设置的字段的概念,而不是getMonth方法等。 Since it was designed long before Java had enum s, each field had a number and a named constant for that number.因为它是在 Java 有enum之前很久设计的,所以每个字段都有一个数字和该数字的命名常量。 So YEAR (of era) was 1, MONTH was 2, etc. To get the value of the field of a Calendar object you were supposed to call the get method passing the appropriate named constant, for example rightNow.get(Calendar.MONTH) .所以YEAR (of era) 是 1, MONTH是 2,等等。要获取Calendar对象字段的值,您应该调用传递适当命名常量的get方法,例如rightNow.get(Calendar.MONTH) . Using just rightNow.MONTH is a regularly repeated mistake.使用rightNow.MONTH是一个经常重复的错误。 It just gives you the value of the constant, 2.它只是为您提供常量 2 的值。

Link关联

Oracle tutorial: Date Time explaining how to use java.time. Oracle 教程:解释如何使用 java.time 的日期时间

    Calendar now = Calendar.getInstance();
    out.println("month now= " + (now.get(Calendar.MONTH)+1));

OR with GregorianCalender:或使用公历日历:

    GregorianCalendar nu  = new GregorianCalendar();
    int day = nu.get(Calendar.DAY_OF_MONTH);
    int month = (nu.get(Calendar.MONTH)+1);  //month +1 because january`==0
    int year= nu.get(Calendar.YEAR);
    int hour= nu.get(Calendar.HOUR_OF_DAY);
    int minutes= nu.get(Calendar.MINUTE);

here这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM