We are going to discuss about new Java 8 API  today. In the recent release of Java 8, new Date and Time API was introduced. It supports ISO (International Organization for Standardization) calendar system. It also supports commonly used global calendars. Let’s quickly get into the example
import java.time.Duration;
import java.time.Instant;

public class TimeEx {
public static void main(String[] args) throws InterruptedException {
Instant start = Instant.now();
System.out.println(start);
Thread.sleep(1000);
Instant end = Instant.now();
System.out.println(end);
Duration elapsed = Duration.between(start,end);
System.out.println("Elapsed: "+elapsed.toMillis()+" milliseconds");
}
}

Output:
2015-01-26T02:36:36.269Z
2015-01-26T02:36:37.518Z
Elapsed: 1249 milliseconds

Firstly we imported Duration and Instant Java classes from the java.time package. This allows us to use required methods in the program. Here we mainly used two Java classes Duration and Instant but we also required to know about Temporal Java class as well i.e. is Instant class is a sub class of Temporal. That the reason why we are able to use Instant object as arguments in the following line even though its asking for Temporal Java class as arguments. You can see the Hierarchy of different classes of this package here in the following link

Instant java class contains static method now() which gives us current time-stamp. Duration java class contains static method between() which allows us to get elapsed time between two time-stamps.
We used toMillis() method with the instance of Duration java class to represent elapsed time between two time-stamps in readable format i.e. milliseconds.

Here is the another program,

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

public class DateTimeValues {
 public static void main(String[] args) {
  LocalDate currentDate = LocalDate.now();
  System.out.println(currentDate);

  LocalDate specificDate = LocalDate.of(2000,1,1);
  System.out.println(specificDate);

  LocalTime currentTime = LocalTime.now();
  System.out.println(currentTime);

  LocalTime specificTime = LocalTime.of(14,0,45);
  System.out.println(specificTime);

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

  LocalDateTime specificDT = LocalDateTime.of(specificDate,specificTime);
  System.out.println(specificDT);
 }
}

Output:
2015-01-26
2000-01-01
09:42:12.908
14:00:45
2015-01-26T09:42:12.908
2000-01-01T14:00:45

Here in the above program,
LocalDate currentDate = LocalDate.now(); is used to display current date.
LocalDate specificDate = LocalDate.of(2000,1,1); is used to display specific date.

Similarly, we display time here
LocalTime currentTime = LocalTime.now(); is used to display current time where as
LocalTime specificTime = LocalTime.of(14,0,45); is used to display specific time.

Similarly we are going to display both date and time using LocalDataTime java class
LocalDateTime currentDT = LocalDateTime.now(); is used to display current date and time.
LocalDateTime specificDT = LocalDateTime.of(specificDate,specificTime); is used to display specific date and time.
Here, we passed LocalDate and LocalTime instances as arguments to the LocalDateTime java class.

You can observe output for better understanding.

Previous versions of Java allows us to work with Date and Time but they are mutable and not thread safe. 
That’s the reason they are leading to potential concurrency errors.
All the classes in the Java 8 Date-Time API are immutable and thread-safe. 
This makes us feel comfortable to use them in multi-threaded environments.

Java 8 Date-Time package is a well-structured API which clearly separates between human readable date, time format and machine time-stamp. 

All the classes used Factory and Strategy design pattern for better handling. 

All the Date-Time API Java classes comes with same methods.
Working with one Java class in this package allows us to feel comfortable to work with other java classes.

Immutable object: Once the object is created its state cannot be modified.

Example: 
String str = “BOSS”;
str.toLowerCase();

Using toLowerCase() method doesn’t change the value of str object instead it creates another object and gives the value boss in its construction.

1 comments:

  1. :d
    It is nice article to improve my knowledge.thank you for sharing useful post
    visit
    web programming tutorial
    welookups

    ReplyDelete

 
Top