-3

There is an anomaly I have observed while setting dates in Java.

I am trying the following:-

Date date1 = new Date("10/12/2018");

So when I am printing the date, it is coming of future It was printing this date:- 14 June 2019.

When I am doing the following:-

Date date1 = new SimpleDateFormat("dd/MM/yyyy").parse("10/12/2018");

It is showing the exact date. Can anyone please explain why this happened? Thanks in advance.

Arnaud
  • 17,229
  • 3
  • 31
  • 44
maverick
  • 82
  • 2
  • 15
  • 6
    `java.util.Date.Date(String)` is deprecated since JDK 1.1 – ernest_k Apr 02 '19 at 06:57
  • 3
    From the documentation: *As of JDK version 1.1, replaced by DateFormat.parse(String s).*. I also suggest having a look at [What's wrong with Java Date & Time API?](https://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api) – Lino Apr 02 '19 at 06:57
  • In the second case, you speciy the format to use when parsing, which matches with the actual format. In the first case, you just assume that the Date constructor will magically use the format you want, instead of reading the javadoc to discover what actual format is supported, and realize that you shouldn't use this constructor in the first place. The documentation is your friend. Read it. – JB Nizet Apr 02 '19 at 07:01

2 Answers2

2

java.time

To hardcode a date use the following:

    LocalDate date2 = LocalDate.of(2018, Month.OCTOBER, 12);
    System.out.println("Harcoded date: " + date2);

You will never get in doubt which is month, day and year. Output is:

Harcoded date: 2018-10-12

You should avoid the poorly designed and long outdated Date class and even more its deprecated constructors. Instead I am using LocalDate from java.time, the modern Java date and time API. Also a LocalDate represents a calendar date (without time of day), contrary to Date, which despite its name represents a point in time.

BTW I could not reproduce your problem. I get Fri Oct 12 00:00:00 CEST 2018 (which also agrees with the documentation, though this part of the documentation is very hard to read and understand).

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

The Date() constructor (Note: it's deprecated !) doesn't know what is month and year.

USA: MM/dd/yyyy while Europe it's dd/MM/yyyy.

In the second form you are explicit. In the the first form it picks the wrong one.

Axel Podehl
  • 4,034
  • 29
  • 41
  • Still it does not make sense to get that result. The documentation is clear: if the string cannot be interpreted it should raise an `IllegalArgumentException`. Now tell me how it can parse that input string and yield a date in 2019 when no `19` is in the input... IMHO it looks like a bug (which wont ever be fixed since it's in a deprecated method, but it's still a bug) – Giacomo Alzetta Apr 02 '19 at 07:05
  • 1
    when I run Date date1 = new Date("10/12/2018"); System.err.println(date1); it prints: "Fri Oct 12 00:00:00 EDT 2018" – Axel Podehl Apr 02 '19 at 07:10
  • The deprecated constructor has decided what is month, day and year: “If the number is followed by a slash, it is regarded as a month (…), unless a month has already been recognized”. So in `10/12/2018`, 10 is the month number, 12 is the day of month and 2018 is the year (obviously). Not that we should bother. We should never use that constructor. And not the clas either (except for a legacy API that we cannot change). – Ole V.V. Apr 02 '19 at 09:40