Monday, January 25, 2016

Using Java 8 Date (LocalDate/Time and LocaDateTime ) API in jdeveloper 12.2.1

Hi All,

It's being long time i did not post any thing on my blog. It's not because of i am busy it's because of i am lazy.As you all know that time never stop.It's keep progressing which make ll other thing to keep change as well.

So Jdeveloper also changed a lot. I used to work on Jdevloper 11.1.1.6. Thereafter i moved to Jdeveloper 12.1.3 (Big move). But suddenly i observed that oracle has released Jdeveloper 12.2.1. So i moved to this version.

The biggest advantage of Jdeveloper is default it support java 8. However Jdevloper 12.3.1 does support Java 8 but that only for deploying application. Jdeveloper it's self used Java 7.0. So in this case we were using both version which was little annoying.

Now we are using Jdeveloper 12.2.1 and i am happy to say that it support Java 8.After generic which they introduced in Java 6 this is biggest paradigm change in Java history. Some time i do feel if i would get chance to work with Java 8.

So there all a lot of changes or i can say a lot of enhancement come with Java 8. The one which every one know that and that is introduction of Lambda expression. To be honest i personally i like this but i will something for this another time. Today my focus is Date API.

Since i started doing programming i never liked Java Date API. I had always filling that if we say Date it's should only be date not time but old Date object always contains time.

So using Java 8 creating data object is simple , more clear and easy to understand.

In Java 8 they introduce whole new package for date and time API. which they called
import java.time; however before this used to be part of  import java.util.*; package.

In lasted version of java they introduced below three new classes for Data and Time.

S.No. Class Name Package Explanation
1 LocalDate java.time.* This class only hold value of Date.
2 LocalTime java.time.* This class only hold value of Time.
3 LocalDateTime java.time.* This class hold value of Date and Time.

So far so good , as name suggest now we have three different classes to handling date and time.
Now let us see and compare how we create data and time object.

The main think which we need to remember here is that all of these classes are immutable and they constructor are private and class are final means we can not used new keyword to create instance of this class.

1-Creating Date /Time and LocalDateTime with current value .
  •  LocalDate.now();
  •  LocalTime.now();
  •  LocalDateTime.now();
 So cool is it ?. So now we have now method in all three classes to create current date/ time and date with time. I got below output.

  • 2016-01-24
  • 18:20:04.277
  • 2016-01-24T18:20:04.396     
The first one printed date without time, second one printed time (hour : minute : second and nanosecond ) without date and last one printed date and time which basically same as Date object. Java use T to separate date and time.

now() is static method which is available in all three classes which we return current value.So far so good now let us see how we create our own date and time.There is another method which is also static and it is present in both three classes called as of().This method will return object of respective type of classes .

2-Creating Date/Time/DateTime object with custom value:

LocalDate onlyDate= LocalDate.of(2011, Month.MARCH, 01);
LocalDate onlyDate= LocalDate.of(2011, 3, 20); //[3 is for March not 2]

Now one think go remember that in old java version the month value was starting from zero which was confusing. However they were trying to make as index value which start zero in java. But here is good news now it will start from one which easy to understand also easy to remember.

LocalTime timeOnly=  LocalTime.of(8, 10, 45, 78);   //[hr , min , second and nanosecond]
LocalDateTime dateTime = LocalDateTime.of(2011, Month.MARCH, 8, 10, 45, 78);

They are also a lot of overloaded method which we can see online java specification.

Next thing to understand how to format date and time. In the old java we have SimpleDateFormat class to format Date object. But in java 8 they have introduced DateTimeFormatter class for format date and time.

All the class has format method which expect object of DateTimeFormatter type.

DateTimeFormatter  f=new DateTimeFormatter ("hh:ss");
timeObject.format(f);

There are still a lot of think to understand this is just a glimpse of java 8.Please share your experience and knowledge.

Thanks,
Prateek