In this post we will be learning new Date and Time API that is being introduced in Java 8. Prior to Java 8 we already have couple of class is available to handle Date and Time like Data, Calendar etc. but these are not convenience to use and also have some performance issue.
Now in Java 8 new Joda time api has been added to overcome all the issue related with Date and Time.
Which is available under Java.time package also know as Joda Time API.
Example - 1
To print local Date and Time.
import Java.time.*; public class Java8DateTime { public static void main(String[] args) {
LocalDate date = LocalDate.now(); System.out.println(date);
LocalTime time=LocalTime.now(); System.out.println(time);
} }
Example - 2
To get the day month and year from local date.
import Java.time.*; class DateAPI{ public static void main(String[] args) { LocalDate date = LocalDate.now(); System.out.println(date); int day = date.getDayOfMonth(); int month = date.getMonthValue(); int year = date.getYear(); System.out.println(day+"..."+month+"..."+year); System.out.printf("\n%d-%d-%d",day,month,year); } }
Check out the video example
EmoticonEmoticon