• No results found

Comparing Date objects

In document JavaNotesForProfessionals.pdf (Page 62-65)

return date;

}

Section 9.8: Creating Date objects

Date date = new Date();

System.out.println(date); // Thu Feb 25 05:03:59 IST 2016

Here this Date object contains the current date and time when this object was created.

Calendar calendar = Calendar.getInstance();

calendar.set(90, Calendar.DECEMBER, 11);

Date myBirthDate = calendar.getTime();

System.out.println(myBirthDate); // Mon Dec 31 00:00:00 IST 1990

Date objects are best created through a Calendar instance since the use of the data constructors is deprecated and discouraged. To do se we need to get an instance of the Calendar class from the factory method. Then we can set year, month and day of month by using numbers or in case of months constants provided py the Calendar class to improve readability and reduce errors.

calendar.set(90, Calendar.DECEMBER, 11, 8, 32, 35);

Date myBirthDatenTime = calendar.getTime();

System.out.println(myBirthDatenTime); // Mon Dec 31 08:32:35 IST 1990

Along with date, we can also pass time in the order of hour, minutes and seconds.

Section 9.9: Comparing Date objects

Calendar, Date, and LocalDate

Version < Java SE 8

before, after, compareTo and equals methods //Use of Calendar and Date objects final Date today = new Date();

final Calendar calendar = Calendar.getInstance();

calendar.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0);

Date birthdate = calendar.getTime();

final Calendar calendar2 = Calendar.getInstance();

calendar2.set(1990, Calendar.NOVEMBER, 1, 0, 0, 0);

Date samebirthdate = calendar2.getTime();

//Before example

System.out.printf("Is %1$tF before %2$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.before(birthdate)));

System.out.printf("Is %1$tF before %1$tF? %3$b%n", today, today, Boolean.valueOf(today.before(today)));

System.out.printf("Is %2$tF before %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(birthdate.before(today)));

//After example

System.out.printf("Is %1$tF after %2$tF? %3$b%n", today, birthdate,

Boolean.valueOf(today.after(birthdate)));

System.out.printf("Is %1$tF after %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.after(today)));

System.out.printf("Is %2$tF after %1$tF? %3$b%n", today, birthdate, Boolean.valueOf(birthdate.after(today)));

//Compare example

System.out.printf("Compare %1$tF to %2$tF: %3$d%n", today, birthdate, Integer.valueOf(today.compareTo(birthdate)));

System.out.printf("Compare %1$tF to %1$tF: %3$d%n", today, birthdate, Integer.valueOf(today.compareTo(today)));

System.out.printf("Compare %2$tF to %1$tF: %3$d%n", today, birthdate, Integer.valueOf(birthdate.compareTo(today)));

//Equal example

System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", today, birthdate, Boolean.valueOf(today.equals(birthdate)));

System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", birthdate, samebirthdate, Boolean.valueOf(birthdate.equals(samebirthdate)));

System.out.printf(

"Because birthdate.getTime() -> %1$d is different from samebirthdate.getTime() -> %2$d, there are millisecondes!%n",

Long.valueOf(birthdate.getTime()), Long.valueOf(samebirthdate.getTime()));

//Clear ms from calendars

calendar.clear(Calendar.MILLISECOND);

calendar2.clear(Calendar.MILLISECOND);

birthdate = calendar.getTime();

samebirthdate = calendar2.getTime();

System.out.printf("Is %1$tF equal to %2$tF after clearing ms? %3$b%n", birthdate, samebirthdate, Boolean.valueOf(birthdate.equals(samebirthdate)));

Version ≥ Java SE 8

isBefore, isAfter, compareTo and equals methods //Use of LocalDate

final LocalDate now = LocalDate.now();

final LocalDate birthdate2 = LocalDate.of(2012, 6, 30);

final LocalDate birthdate3 = LocalDate.of(2012, 6, 30);

//Hours, minutes, second and nanoOfsecond can also be configured with an other class LocalDateTime //LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond);

//isBefore example

System.out.printf("Is %1$tF before %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isBefore(birthdate2)));

System.out.printf("Is %1$tF before %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isBefore(now)));

System.out.printf("Is %2$tF before %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(birthdate2.isBefore(now)));

//isAfter example

System.out.printf("Is %1$tF after %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isAfter(birthdate2)));

System.out.printf("Is %1$tF after %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isAfter(now)));

System.out.printf("Is %2$tF after %1$tF? %3$b%n", now, birthdate2, Boolean.valueOf(birthdate2.isAfter(now)));

//compareTo example

System.out.printf("Compare %1$tF to %2$tF %3$d%n", now, birthdate2, Integer.valueOf(now.compareTo(birthdate2)));

System.out.printf("Compare %1$tF to %1$tF %3$d%n", now, birthdate2, Integer.valueOf(now.compareTo(now)));

System.out.printf("Compare %2$tF to %1$tF %3$d%n", now, birthdate2, Integer.valueOf(birthdate2.compareTo(now)));

//equals example

System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.equals(birthdate2)));

System.out.printf("Is %1$tF to %2$tF? %3$b%n", birthdate2, birthdate3, Boolean.valueOf(birthdate2.equals(birthdate3)));

//isEqual example

System.out.printf("Is %1$tF equal to %2$tF? %3$b%n", now, birthdate2, Boolean.valueOf(now.isEqual(birthdate2)));

System.out.printf("Is %1$tF to %2$tF? %3$b%n", birthdate2, birthdate3, Boolean.valueOf(birthdate2.isEqual(birthdate3)));

Date comparison before Java 8

Before Java 8, dates could be compared using java.util.Calendar and java.util.Date classes. Date class offers 4 methods to compare dates :

after(Date when) before(Date when)

compareTo(Date anotherDate) equals(Object obj)

after, before, compareTo and equals methods compare the values returned by getTime() method for each date.

compareTo method returns positive integer.

Value greater than 0 : when the Date is after the Date argument Value greater than 0 : when the Date is before the Date argument Value equals to 0 : when the Date is equal to the Date argument

equals results can be surprising as shown in the example because values, like milliseconds, are not initialize with the same value if not explicitly given.

Since Java 8

With Java 8 a new Object to work with Date is available java.time.LocalDate. LocalDate implements

ChronoLocalDate, the abstract representation of a date where the Chronology, or calendar system, is pluggable.

To have the date time precision the Object java.time.LocalDateTime has to be used. LocalDate and LocalDateTime use the same methods name for comparing.

Comparing dates using a LocalDate is different from using ChronoLocalDate because the chronology, or calendar system are not taken in account the first one.

Because most application should use LocalDate, ChronoLocalDate is not included in examples. Further reading here.

Most applications should declare method signatures, fields and variables as LocalDate, not this[ChronoLocalDate] interface.

LocalDate has 5 methods to compare dates :

isAfter(ChronoLocalDate other) isBefore(ChronoLocalDate other) isEqual(ChronoLocalDate other) compareTo(ChronoLocalDate other) equals(Object obj)

In case of LocalDate parameter, isAfter, isBefore, isEqual, equals and compareTo now use this method:

int compareTo0(LocalDate otherDate) { int cmp = (year - otherDate.year);

if (cmp == 0) {

cmp = (month - otherDate.month);

if (cmp == 0) {

cmp = (day - otherDate.day);

} }

return cmp;

}

equals method check if the parameter reference equals the date first whereas isEqual directly calls compareTo0. In case of an other class instance of ChronoLocalDate the dates are compared using the Epoch Day. The Epoch Day count is a simple incrementing count of days where day 0 is 1970-01-01 (ISO).

In document JavaNotesForProfessionals.pdf (Page 62-65)