Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java's Date.getYear() return 111 instead of 2011?

I am having a bit of trouble parsing a string date to a Date object. I use a DateFormat to parse the string, and when I print the value of the date, it gives me what I expect.

But when I try get the day, the month or the year it gives me the wrong values. For instance, the year is 2011, but when I do .getYear() it gives me 111. I have no idea why this is happening. Here is the relevant code segment:

    Date dateFrom = null;      String gDFString = g.getDateFrom();      System.out.println(gDFString);      DateFormat df = new SimpleDateFormat("dd/MM/yyyy");      try {         dateFrom = df.parse("04/12/2011");          System.out.println(dateFrom);          System.out.println(dateFrom.getYear());     } catch (ParseException e) {         e.printStackTrace();     } 

When I out print dateFrom, I get Sun Dec 04 00:00:00 GMT 2011, which is what you would expect. But printing .getYear() returns 111.

I need to be able to get the day, month and year of the date for a time series graph.

like image 370
Ben Calder Avatar asked Aug 27 '11 15:08

Ben Calder


People also ask

How do you return a date in Java?

String In Standard Format, ISO 8601 If you want to return the date information as text, use the Joda-Time library or the new java. time package in Java 8 to convert a date-time value into a String using the format defined by the ISO 8601 standard. Those two libraries use ISO 8601 format by default.

How does Java define date of birth?

Here is an example: String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); Date date = simpleDateFormat. parse("2018-09-09"); Once this code is executed, the date variable points to a Date instance representing september 9th, 2018.

How do I get the current year from a date in Java?

To get the previous year in Java, first we need to access the current year using the Year. now(). getValue() method and subtract it with -1 . Note: The getValue() method returns the current year in four-digit(2021) format according to the user's local time.


2 Answers

Those methods have been deprecated. Instead, use the Calendar class.


import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar;  public final class DateParseDemo {     public static void main(String[] args){          final DateFormat df = new SimpleDateFormat("MM/dd/yyyy");          final Calendar c = Calendar.getInstance();          try {              c.setTime(df.parse("04/12/2011"));              System.out.println("Year = " + c.get(Calendar.YEAR));              System.out.println("Month = " + (c.get(Calendar.MONTH)));              System.out.println("Day = " + c.get(Calendar.DAY_OF_MONTH));          }           catch (ParseException e) {              e.printStackTrace();          }     } } 

Output:

Year = 2011 Month = 3 Day = 12 

And as for the month field, this is 0-based. This means that January = 0 and December = 11. As stated by the javadoc,

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

like image 196
mre Avatar answered Oct 04 '22 01:10

mre


Javadoc to the rescue:

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.

You should not use deprecated methods. Deprecated methods are methods which should not be used anymore. But whatever the method you're using, read its javadoc to know what it does.

like image 38
JB Nizet Avatar answered Oct 04 '22 01:10

JB Nizet