java.util.Date and getYear()

前端 未结 12 919
生来不讨喜
生来不讨喜 2020-12-08 13:01

I am having the following problem in Java (I see some people are having a similar problem in JavaScript but I\'m using Java)

System.out.println(new Date().ge         


        
相关标签:
12条回答
  • 2020-12-08 13:19

    The java documentation suggests to make use of Calendar class instead of this deprecated way Here is the sample code to set up the calendar object

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    

    Here is the sample code to get the year, month, etc.

    System.out.println(calendar.get(Calendar.YEAR));
    System.out.println(calendar.get(Calendar.MONTH));
    

    Calendar also has support for many other useful information like, TIME, DAY_OF_MONTH, etc. Here the documentation listing all of them Please note that the month are 0 based. January is 0th month.

    0 讨论(0)
  • 2020-12-08 13:19

    Year.now()

    There's an easier way to use the java.time library in Java 8+. The expression:

    java.time.Year.now().getValue()
    

    returns the current year as a four-digit int, using your default time zone. There are lots of options for different time ZoneIds, Calendars and Clocks, but I think this is what you will want most of the time. If you want the code to look cleaner (and don't need any other java.time.*.now() functions), put:

    import static java.time.Year.now;
    

    at the top of your file, and call:

    now().getValue()
    

    as needed.

    0 讨论(0)
  • 2020-12-08 13:23

    According to javadocs:

    @Deprecated
    public int getYear()
    

    Deprecated. As of JDK version 1.1, replaced by Calendar.get(Calendar.YEAR) - 1900.

    Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.

    Returns: the year represented by this date, minus 1900.

    See Also: Calendar

    So 112 is the correct output. I would follow the advice in the Javadoc or use JodaTime instead.

    0 讨论(0)
  • 2020-12-08 13:31

    Yup, this is in fact what's happening. See also the Javadoc:

    Returns:
       the year represented by this date, minus 1900.
    

    The getYear method is deprecated for this reason. So, don't use it.

    Note also that getMonth returns a number between 0 and 11. Therefore, this.sale.getSaleDate().getMonth() returns 1 for February, instead of 2. While java.util.Calendar doesn't add 1900 to all years, it does suffer from the off-by-one-month problem.

    You're much better off using JodaTime.

    0 讨论(0)
  • 2020-12-08 13:31

    Java 8 LocalDate class is another option to get the year from a java.util.Date,

    int year = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date)).getYear();
    

    Another option is,

    int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
    
    0 讨论(0)
  • 2020-12-08 13:38

    Don't use Date, use Calendar:

    // Beware: months are zero-based and no out of range errors are reported
    Calendar date = new GregorianCalendar(2012, 9, 5);
    int year = date.get(Calendar.YEAR);  // 2012
    int month = date.get(Calendar.MONTH);  // 9 - October!!!
    int day = date.get(Calendar.DAY_OF_MONTH);  // 5
    

    It supports time as well:

    Calendar dateTime = new GregorianCalendar(2012, 3, 4, 15, 16, 17);
    int hour = dateTime.get(Calendar.HOUR_OF_DAY);  // 15
    int minute = dateTime.get(Calendar.MINUTE);  // 16
    int second = dateTime.get(Calendar.SECOND);  // 17
    
    0 讨论(0)
提交回复
热议问题