-2

I'm trying to get year from the Date, and with Calendar.get(Calendar.YEAR) i get year 3917 instead of 2017 ? But when i try to check output with date.getYear() it returns the correct year.

Date date = new Date(year, month, day);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
Log.d(TAG, "onClick: year "+date.getYear());
Log.d(TAG, "onClick: year "+calendar.get(Calendar.YEAR));

Output:

onClick: year 2017

onClick: year 3917

Date class displays the correct year but Calendar displays wrong year

Community
  • 1
  • 1
Miljan Rakita
  • 1,433
  • 4
  • 23
  • 44
  • You can read the explanation here https://stackoverflow.com/questions/26255636/why-does-java-util-date-represent-year-as-year-1900 – tomas Sep 01 '17 at 09:07
  • 2
    Don't use Date, use latest API `LocalDate` – azro Sep 01 '17 at 09:13
  • 2
    You should use [`LocalDate.of`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#of-int-int-int-) instand of this outdated solution. Then use [`LocalDate.getYear()`](https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html#getYear--). – AxelH Sep 01 '17 at 09:14
  • @PeterBruins In my problem as you can see date.getYear() works, but Calendar.YEAR is not working properly. On the provided link there is problem with date.getYear() not with Calendar.Year as it is in my problem – Miljan Rakita Sep 01 '17 at 09:25
  • 2
    @MiljanRakita no it doesn't return what you want. The doc of `Date` said that it use a value of year from `1900` so it should give you `117` for a date to the year 2017. You are not using it right, since `Calendar` read the doc to understand the value received, it adds `1900` to 2017 to get the equivalent date, giving 3917. You are just using it wrong. – AxelH Sep 01 '17 at 09:33

3 Answers3

5

Here's the Javadoc of Date class, this is what it says about the constructor that you are using:

Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).

Allocates a Date object and initializes it so that it represents midnight, local time, at the beginning of the day specified by the year, month, and date arguments.

Also, here's the explanation for parameters:

Parameters:

year - the year minus 1900.

month - the month between 0-11.

date - the day of the month between 1-31.

As it adds 1900 to the value you pass, the resultant year you are getting is 3917.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Since we are here (and the value are already there), month will be incorrect too in his code since it is 0-based (in both class). – AxelH Sep 01 '17 at 09:40
3

As said in other answers, Date year is a number of year from 1900 (more information in the complete answer of Darshan Mehta).

But there is a simpler solution using LocalDate.of to create a date to year, month, day and using LocalDate.getYear to get the specific year :

java.time.LocalDate date = java.time.LocalDate.of(2017, 9, 1);
System.out.println(date.getYear());

2017

java.util.Date is outdated (and all the method you used are deprecated), you should look to the java.time api that is more readable and functional

AxelH
  • 14,325
  • 2
  • 25
  • 55
1

Method of Date:

public Date(int year, int month, int day) {
    GregorianCalendar cal = new GregorianCalendar(false);
    cal.set(1900 + year, month, day);
    milliseconds = cal.getTimeInMillis();
    }

As you can see, 1900 are added to your calendar date. Which results in 3917

Jagson
  • 23
  • 4