A new date and time API has been designed for Java SE 8.



There are several problems with existing Date and Time API some of them are-
1. The existing classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe
2. Poor API design. E.g. years in java.util.Date start at 1900, months start at 1, and days start at 0.


So, a new date and time API, which is free of these problems, has been designed for Java SE 8.
The new API allows people to work with different calendaring systems in order to support the needs of users in some areas of the world.
Java 8 introduces a new date-time API under the package java.time. LocalDate, LocalTime and LocalDateTime classes simplify the development where timezones are not required. They are local in the sense that they represent date and time from the context of the observer, such as a calendar on a desk or a clock on your wall.
E.g.
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.Month;

public class DateTimeTester {
   public static void main(String args[]){
   DateTimeTester  tester = new DateTimeTester();
      tester.testLocalDateTime();
   }

   public void testLocalDateTime(){

      LocalDateTime currentTime = LocalDateTime.now();
      System.out.println("Current DateTime: " + currentTime);

      LocalDate d1 = currentTime.toLocalDate();
      System.out.println("Current Date: " + d1);

      Month month = currentTime.getMonth();
      int day = currentTime.getDayOfMonth();
      int seconds = currentTime.getSecond();

      System.out.println("Month: " + month +" day: " + day +" seconds: " + seconds);

      LocalDate d2 = LocalDate.of(2016, Month.SEPTEMBER, 9);
      System.out.println("Set Date: " + d2);
   
      LocalTime t1 = LocalTime.of(22, 15);
      System.out.println("Set Time: " + t1);

      }
}

Output:
Current DateTime: 2016-09-09T11:00:45.457
Current Date: 2016-09-09
Month: SEPTEMBER day: 9 seconds: 45
Set Date: 2016-09-09
Set Time: 22:15


Standard Java getter method is used in order to obtain values from Java SE 8 classes. You can also alter the object values in order to perform calculations. Because all core classes are immutable in the new API, these methods are called with and return new objects, rather than using setters.
Zoned date-time API is to be used when time zone is to be considered.
 E.g.
import java.time.ZonedDateTime;
import java.time.ZoneId;

public class Tester {
   public static void main(String args[]){
      Tester tester = new Tester();
      tester.testZonedDateTime();
   }

   public void testZonedDateTime(){

      // Get the current date and time
      ZonedDateTime date1 = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Paris]");
      System.out.println("date1: " + date1);

      ZoneId id = ZoneId.of("Asia/Karachi ");
      System.out.println("ZoneId: " + id);

      ZoneId currentZone = ZoneId.systemDefault();
      System.out.println("CurrentZone: " + currentZone);
   }
}

ZonedDateTime is a date and time with a fully qualified time zone.

Output:
date1: 2007-12-03T10:15:30+01:00[Europe/Paris]
ZoneId: Asia/Karachi
CurrentZone: Etc/UTC

No comments:

Post a Comment