-2

My code:

DateFormat format = new SimpleDateFormat("dd/MM/YYYY");

Date Today = format.parse(today);
Date Date = format.parse(date);

difference = (Date.getTime() - Today.getTime()) / 86400000;
Math.abs(difference);
System.out.println(String.valueOf("date" + date));
System.out.println(String.valueOf("date" + Date));
System.out.println(String.valueOf("date" + today));
System.out.println(String.valueOf("date" + Today));

The output:
date29/11/2016
dateSun Dec 27 00:00:00 GMT+08:00 2015
date20/11/2016
dateSun Dec 27 00:00:00 GMT+08:00 2015

I have problems while parsing the date, the original date is 29/11/2016, however when parsed, the date becomes Sun Dec 27 00:00:00 GMT+08:00 2015.

This problem appears in my whole program whenevr it's related to date.

enter image description here

Jeosiyy Chu
  • 67
  • 2
  • 15

2 Answers2

1

Format dd/MM/YYYY should be dd/MM/yyyy. See javadoc of SimpleDateFormat: y = Year, Y = Week year. – Andreas 12 mins ago

Thanks resolved.

Jeosiyy Chu
  • 67
  • 2
  • 15
1

I know you got your solution from Andreas’ comment already. What I want to do here is take a step back and suggest a few improvements to your code. Feel free to ignore.

  1. Most importantly I suggest you throw the long outdated classes Date, DateFormat and SimpleDateFormat overboard and start using their modern replacements. These came out in the java.time package early in 2014. These also offer a much more straightforward and clear way of calculating the difference.
  2. Follow the convention that says that a variable name begins with a lowercase letter. Specifically, in the same source file to use a class called Date and two variable called date and Date is bound to cause confusion.
  3. Like Henry I also think you intended difference = Math.abs(difference);.
  4. Your calls to String.valueOf() are superfluous and just seem to make the code a bit harder to read. Drop them.

For the sake of the example, in the code I suggest below, I am deliberately using your incorrect date format pattern string, dd/MM/YYYY.

    DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/YYYY");

    LocalDate todayAsLocalDate = LocalDate.parse(today, format);
    LocalDate dateAsLocalDate = LocalDate.parse(date, format);

    difference = ChronoUnit.DAYS.between(todayAsLocalDate, dateAsLocalDate);
    difference = Math.abs(difference);
    System.out.println("date " + date);
    System.out.println("date " + dateAsLocalDate);
    System.out.println("date " + today);
    System.out.println("date " + todayAsLocalDate);
    System.out.println(difference);

As the code stands now, it throws a java.time.format.DateTimeParseException: Text '20/11/2016' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2016, MonthOfYear=11, DayOfMonth=20},ISO of type java.time.format.Parsed. When the code is incorrect, I much prefer an exception over an incorrect result that might go unnoticed. So this is better than what SimpleDateFormat gave you.

While the message is not easy to read, the bit to notice is WeekBasedYear. Week-based years are only useful with week numbers, you intended none of that. If you compare with the documentation, you will see that uppercase Y in the pattern is week-based-year while lowercase y is year-of-era. So let’s correct:

    DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");

Now the code prints:

date 29/11/2016
date 2016-11-29
date 20/11/2016
date 2016-11-20
9

We note one final advantage of the modern classes: you can have a date without a time-of-day when this is what you need, again giving code that models your requirements more precisely and thereby leaves less room for confusion.

Question: can I use the modern classes with my Java version?

If using at least Java 6, you can.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161