Saturday, March 3, 2018

Date and Time API: Java 8

Until java 1.7 version the classes present in java.util package to handle Date and Time(like Date, Calendar, TimeZone etc) are not upto the mark with respect to convenience and performance.

To overcome this problem in the 1.8 version oracle people introduced Joda-Time API. This API developed by joda.org and available in java in the form of java.time package.

# program for to display System Date and Time.

import java.time.*;
   public class DateTime {
     public static void main(String[] args) {
       LocalDate date = LocalDate.now();
       System.out.println(date);
       LocalTime time = LocalTime.now();
       System.out.println(time);
   }
 } 

Output:

2015-11-23
12:39:26:587

Once we get LocalDate object we can call the following methods on that object to retrieve Day, month and year values separately.

Ex:

import java.time.*;
class Test {
     Public static void main(Strng[] 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);
         System.out.println(“\n%d-%d-%d,dd,mm,yy);
     }
}   

Once we get LocalTime object we can call the following methods on that object.

Ex:

import java.time.*;
class Test {
    public static void main(String[] args) {
         LocalTime time = LocalTime.now();
         int h = time.getHour();
         int m = time.getMinute();
         int s = time.getSecond();
         int n = time.getNano();
         System.out.println(“\n%d:%d:%d:%d,h,m,s,n);
    }
}

If we want to represent both Date and Time then we should go for LocalDateTime object.

LocalDateTime dt = LocalDateTime.now();
   System.out.println(dt); 

Output:

2015-11-23T12:57:24:531

We can represent a particular Date and Time by using LocalDateTime object as follows.

Ex:

LocalDateTime dt1=LocalDateTime.of(1995,Month.APRIL,28,12,45);
   sop(dt1);  

Ex:

LocalDateTime dt1=LocalDateTime.of(1995,04,28,12,45);
   sop(dt1);
   sop(“After six months:”+dt.plusMonths(6));
   sop(“Before six months:”+dt.minusMonths(6));  

To Represent Zone

ZoneId object can be used to represent Zone.

Ex:

import java.time.*;
class ProgramOne {
   public static void main(String[] args) {
       ZoneId zone = ZoneId.systemDefault();
       System.out.println(zone);
   }
}

We can create ZoneId for a particular zone as follows

Ex:

ZoneId la = ZoneId.of(“America/Los_Angeles”);
   ZoneIdDateTime zt = ZoneIdDateTime.now(la);
   System.out.println(zt);

Period Object

Period object can be used to represent quantity of time.

Ex:

LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1989,06,15);
Period p = Period.between(birthday,today);
System.out.println(age is %d year %d months %d days,p.getYears(),p.getMonths(),p.getDays());

# write a program to check the given year is leap year or not.

import java.time.*;
public class Leapyear {
   int n = Integer.parseInt(args[0]);
   Year y = Year.of(n);
   if(y.isLeap())  
      System.out.println(%d is Leap year,n);
   else
      System.out.println(%d is not Leap year,n);
}

Thanks for reading. If you like this post please follow us for more updates about technology related updates.

No comments: