'java.util.Date toString method returns a String with wrong year: 3920 instead of 2020 [duplicate]
I have this code:
Date date = new Date(2020, 11, 1);
System.out.println(date); // Wed Dec 01 00:00:00 XXX 3920
System.out.println(date.getYear()); // 2020
System.out.println(date.getMonth()); // 11
System.out.println(date.getDay()); // 3
As can be seen, it prints year 3920
instead of 2020
. Why does it happen? Any way to fix it without radically changing the code?
PS I know the constructor and the getter methods are deprecated. Unless that is the reason for the wrong output, I'd prefer to keep using them in this particular example.
Solution 1:[1]
tl;dr
LocalDate.of( 2020, Month.DECEMBER, 1 )
Avoid legacy date-time classes
You are using a terrible date-time class, Date
, which has many tragic design flaws. One of those flaws is an offset of 1900 years.
The solution is simple: Stop using that class. It is now legacy, supplanted years ago by the modern java.time classes defined in JSR 310.
Plus, the java.util.Date
class does not fit your need. That class represents a moment, a date with time of day as seen in UTC, an offset of zero hours minutes seconds. But you want a date-only, without time of day, without the context of an offset or time zone.
The other Date
,java.sql.Date
class, pretends to fit your need as a date-only value. But actually that class inherited from java.util.Date
in an extremely poor design choice.
Use LocalDate
Instead, use java.time.LocalDate
.
This class uses sane numbering:
- The year 2020 is 2020.
- The months January through December are 1 to 12.
Instantiate via static
factory methods.
LocalDate ld = LocalDate.of( 2020, 12, 1 ) ;
Or use the Month
enum.
LocalDate ld = LocalDate.of( 2020, Month.DECEMBER, 1 ) ;
Interrogate for parts.
int y = ld.getYear() ;
int m = ld.getMonthValue() ;
String month = ld.etMonth().getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ;
int d = ld.getDayOfMonth() ;
String dow = ld.getDayOfWeek().getDisplayName( TextStyle.FULL , Locale.JAPAN ) ;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |