Saturday 15 August 2015

Java8: New Date and Time API

Java8 comes with new date and time apis, to overcome the complications prior to Java8. Java8 package include new classes like Period, LocalDate, LocalTime, LocalDateTiime etc., to make your work easier.

Work with LocalDate
LocalDate stores date without time such as 2015-07-22 (year-month-day format). LocalDate class is immutable and thread safe.

LocaDate class provides number of factory methods like of, now, ofEpochDay, ofYearDay, parse to get instance if LocalDate.
Once you get the instance of LocalDate class, you can access year, month, day, day of the week etc.,

import java.time.LocalDate;

public class LocalFDateEx {

 public static void printLocalDateInfo(LocalDate date) {
  System.out.println("Day Of the Month : " + date.getDayOfMonth());
  System.out.println("Day Of Year : " + date.getDayOfYear());
  System.out.println("Month value : " + date.getMonthValue());
  System.out.println("Month : " + date.getMonth());
  System.out.println("Year : " + date.getYear());
  System.out.println("Length Of Month : " + date.lengthOfMonth());
  System.out.println("Length Of Year :  " + date.lengthOfYear());
  System.out.println("Is leap year : " + date.isLeapYear());
 }

 public static void main(String args[]) {
  LocalDate date = LocalDate.of(2016, 07, 22);
  printLocalDateInfo(date);
 }
}


Output

Day Of the Month : 22
Day Of Year : 204
Month value : 7
Month : JULY
Year : 2016
Length Of Month : 31
Length Of Year :  366
Is leap year : true


Obtain LocalDate using System clock

LocalDate class provide static now() method, which obtains the current date from the system clock in the default time-zone.

import java.time.LocalDate;

public class LocalFDateEx {

 public static void printLocalDateInfo(LocalDate date) {
  System.out.println("Day Of the Month : " + date.getDayOfMonth());
  System.out.println("Day Of Year : " + date.getDayOfYear());
  System.out.println("Month value : " + date.getMonthValue());
  System.out.println("Month : " + date.getMonth());
  System.out.println("Year : " + date.getYear());
  System.out.println("Length Of Month : " + date.lengthOfMonth());
  System.out.println("Length Of Year :  " + date.lengthOfYear());
  System.out.println("Is leap year : " + date.isLeapYear());
 }

 public static void main(String args[]) {
  LocalDate date = LocalDate.now();
  printLocalDateInfo(date);
 }
}


Output

Day Of the Month : 22
Day Of Year : 203
Month value : 7
Month : JULY
Year : 2015
Length Of Month : 31
Length Of Year :  365
Is leap year : false


LocalDate class provides get method, which takes TemporalField as input and gives the value of the specified field from this date as an int. TemporalField is an interface, ChronoField implements this interface, contains standard set of fields. LocalDate class supports following fields.

DAY_OF_WEEK
ALIGNED_DAY_OF_WEEK_IN_MONTH
ALIGNED_DAY_OF_WEEK_IN_YEAR
DAY_OF_MONTH
DAY_OF_YEAR
ALIGNED_WEEK_OF_MONTH
ALIGNED_WEEK_OF_YEAR
MONTH_OF_YEAR
YEAR_OF_ERA
YEAR
ERA

import java.time.LocalDate;
import java.time.temporal.ChronoField;

public class LocalFDateEx {

 public static void printLocalDateInfo(LocalDate date) {
  System.out.println(date.get(ChronoField.DAY_OF_WEEK));
  System.out.println(date.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH));
  System.out.println(date.get(ChronoField.ALIGNED_DAY_OF_WEEK_IN_YEAR));
  System.out.println(date.get(ChronoField.DAY_OF_MONTH));
  System.out.println(date.get(ChronoField.DAY_OF_YEAR));
  System.out.println(date.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
  System.out.println(date.get(ChronoField.ALIGNED_WEEK_OF_YEAR));
  System.out.println(date.get(ChronoField.MONTH_OF_YEAR));
  System.out.println(date.get(ChronoField.YEAR_OF_ERA));
  System.out.println(date.get(ChronoField.YEAR));
  System.out.println(date.get(ChronoField.ERA));

 }

 public static void main(String args[]) {
  LocalDate date = LocalDate.of(1988, 6, 6);
  printLocalDateInfo(date);
 }
}


Output

1
6
4
6
158
1
23
6
1988
1988
1



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment