Oracle Certified Associate: Java 8 Maniplating Date and Time


In last post on java.time package basics we saw how to create date and time objects. In this post we will see how to manipulate date and time objects.
From previous post we know how to create Date object, lets us try to get month, year and date from the object.

LocalDate, LocalTime and LocalDateTime provides methods to get or divide date and time in individual elements. Methods name are very intuitive. For example to get year from date use getYear method of LocalDate, to get day of week use getDayofWeek method of LocalDate, following is sample code for the same.

LocalDate d = LocalDate.of(2016,02,20);
System.out.println(d.getDayOfMonth()); //20
System.out.println(d.getDayOfWeek());  // SATURDAY
System.out.println(d.getMonthValue()); // 2
System.out.println(d.getYear()); //2016

To find whether the year is leap year use isLeapYear method which returns boolean value.

System.out.println(d.isLeapYear());  // true

To find the number of days in a year or in a month of date use lengthOfYear and lengthOfMonth methods.

System.out.println(d.lengthOfYear());    //366
System.out.println(d.lengthOfMonth());  //29

To add or subtract days, weeks, months, years methods like minuDays, plusDays etc.. can be used. R Remember instances are so any operation that changes the date and time will return the new instance leaving original instance unchanged. Following is example of adding 20 in days, weeks, months and years and subtracting the same. Each call is creating new instance of LocalDate.

System.out.println(d.minusDays(20));
System.out.println(d.minusWeeks(20));
System.out.println(d.minusMonths(20));
System.out.println(d.minusYears(20));

System.out.println(d.plusDays(20));
System.out.println(d.plusWeeks(20));
System.out.println(d.plusMonths(20));
System.out.println(d.plusYears(20));

To compare two dates there are three methods isBefore, isAfter, isEqual which returns

System.out.println(d.isBefore(LocalDate.of(2016,02,25)));
System.out.println(d.isAfter(LocalDate.of(2016,02,25)));
System.out.println(d.isEqual(LocalDate.of(2016,02,20)));

Similarly like LocalDate , there are methods for LocalTime

To get the Minutes, Hours, Seconds and NanoSeconds use : getMinute(), getHour(), getSecond(), getNano().

To add or subtract use plusHours(), plusMinutes(), plusSeconds(), plusNanos(), minusHours(), minusMinutes(), minusSeconds(), minusNanos()

To compare time use isBefore(), isAfter(), isEqual().

To extract the time in nanoseconds of day toNanoOfDay() and to extract time in seconds of day use toSecondsOfDay() both returns long value representing nano seconds till time of day or seconds till time of day.

LocalDateTime is combination of LocalDate and LocalTime so all the methods can be used with LocalDateTime. We can also chain the date and time method. Consider for example

LocalDate ld = LocalDate.of(2016,04,20);
LocalTime lt = LocalTime.of(10,50);
LocalDateTime temp = LocalDateTime.of(ld,lt).minusDays(10).minusMonths(2);
System.out.println(temp); // 2016-02-10T10:50

Apart from all this java.time provides a class name Period which is used to define the period and can be used as example annually period, monthly, quarterly etc… Let us see with example

Period m = Period.ofMonths(2);
LocalDate ld = LocalDate.of(2016,04,20);
LocalDate x = ld.plus(m); // Add period of two months
System.out.println(x); // 2016-06-20

Same way like ofMonths, ofYears, ofWeeks, ofDays can also be used to create Period. Methods can be chained like
Period w = Peroid.ofYears(1).ofWeeks(1); //equivalent to every week.

In next post we will learn about how to format and parse date and time.

Leave a comment

Your email address will not be published. Required fields are marked *