Recent Posts

Saturday, 10 November 2018

Date and Time API


Date and Time API
     Date and Time API also calle Joda-Time API. Until Java 1.7version the classes present in Java.util package to handle Date and Time (like Date, Calendar, TimeZoneetc) are not up to the mark with respect to convenience and performance. To overcome this problem in the 1.8version oracle people introduced Joda-Time API. This API developed by joda.org and available in Java in the form of Java.time package.
package com.ashok.java8.datetime;

import java.time.LocalDate;
import java.time.LocalTime;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class DateTimeTest {
   public static void main(String[] args) {
      LocalDate date = LocalDate.now();
      System.out.println(date);
      LocalTime time = LocalTime.now();
      System.out.println(time);
   }
}
Output
2018-11-10
21:19:28.388
     Once we get LocalDate object we can call the following methods on that object to retrieve Day,month and year values separately.
package com.ashok.java8.datetime;

import java.time.LocalDate;
/**
 * 
 * @author ashok.mariyala
 *
 */
public class TestDateTime {
   public static void main(String[] args) {
      LocalDate date = LocalDate.now();
      System.out.println(date);
      int dd = date.getDayOfMonth();
      int mm = date.getMonthValue();
      int yy = date.getYear();
      
      System.out.println(dd+"-"+mm+"-"+yy);
   }
}
Output
2018-11-10
10-11-2018
     If we want to represent both Date and Time then we should go for LocalDateTime object.
package com.ashok.java8.datetime;

import java.time.LocalDateTime;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      LocalDateTime dt = LocalDateTime.now(); 
      System.out.println(dt);
   }
}
Output
2018-11-10T21:26:27.387
     We can represent a particular Date and Time by using LocalDateTime object as follows.
package com.ashok.java8.datetime;

import java.time.LocalDateTime;
import java.time.Month;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Test {
   public static void main(String[] args) {
      LocalDateTime dt1 = LocalDateTime.of(2016, Month.NOVEMBER, 28, 12, 45);
      System.out.println(dt1);

      LocalDateTime dt2 = LocalDateTime.of(2018, 04, 28, 12, 45);
      System.out.println(dt2);
      System.out.println("After six months:" + dt2.plusMonths(6));
      System.out.println("Before six months:" + dt2.minusMonths(6));
   }
}
Output
2016-11-28T12:45
2018-11-28T12:45
After six months : 2019-05-28T12:45
Before six months : 2018-05-28T12:45
To Represent Zone
     ZoneId object can be used to represent Zone. We can create ZoneId for a particular zone as follows
package com.ashok.java8.datetime;

import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ZoneTest {
   public static void main(String[] args) {
      ZoneId zone = ZoneId.systemDefault();
      System.out.println(zone);
      
      ZoneId la = ZoneId.of("America/Los_Angeles");
      ZonedDateTime zt = ZonedDateTime.now(la);
      System.out.println(zt);
   }
}
Output
Asia/Calcutta
2018-11-10T08:09:51.768-08:00[America/Los_Angeles]
Period Object
     Period object can be used to represent quantity of time.
package com.ashok.java8.datetime;

import java.time.LocalDate;
import java.time.Period;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class PeriodTest {
   public static void main(String[] args) {
      LocalDate today = LocalDate.now();
      LocalDate birthday = LocalDate.of(1991, 06, 15);
      Period p = Period.between(birthday, today);
      System.out.printf("Age is %d year %d months %d days", p.getYears(), p.getMonths(), p.getDays());
   }
}
Output
Age is 27 year 4 months 26 days
isLeap()
    isLeap() function is used to check given year is leap year or not.
package com.ashok.java8.datetime;

import java.time.Year;
import java.util.Scanner;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class LeapYearTest {
   public static void main(String[] args) {
      try(Scanner scan = new Scanner(System.in)) {
         System.out.print("Enter Year : ");
         int n = scan.nextInt();
         Year y = Year.of(n);
         
         if(y.isLeap())
            System.out.printf("%d is Leap year",n); 
         else 
            System.out.printf("%d is not Leap year",n);
      }
   }
}
Output
Enter Year : 2020
2020 is Leap year

     So, this was all about New Features of Java 8. Hope you like our explanation.

Previous Tutorial : Java 8 Streams
 

No comments:

Post a Comment