InterviewSolution
Saved Bookmarks
| 1. |
New Date/Time API in Java 8 |
|
Answer» Java 8 introduced a new Date/Time Application Program Interface(API) as the previous Date/Time API w drawbacks.
Let us see an EXAMPLE of the Local class with the new Date/Time API: import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; import java.time.Month; public class Example { public void checkDate() { LocalDateTime currentTime = LocalDateTime.now(); // computing local Date/Time System.out.println("Present DateTime: " + currentTime); LocalDate date = currentTime.toLocalDate(); // computing local Data System.out.println("Present Local Date : " + date); // computing current local time in hours, minutes and seconds int second = currentTime.getSecond(); int minute =currentTime.getMinute(); int hour=currentTime.getHour(); System.out.println("Hour: " + hour +"|Minute: " + minute +"|seconds: " + second); } public STATIC void main(String args[]) { Example obj = new Example(); obj.checkDate(); } }The output is as follows: $javac Example.java $java Example Present DateTime: 2018-12-13T18:39:24.730 Present Local Date : 2018-12-13 Hour: 18|Minute: 39|seconds: 24Now, let us see an example of the Zoned class with the new Date/Time API: import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Example { public static void Zone() { LocalDateTime dt = LocalDateTime.now(); DateTimeFormatter format = DateTimeFormatter.ofPattern(" HH:mm:ss dd-MM-YYYY"); String fcd = dt.format(format); // stores the formatted current date System.out.println("Present formatted Time and Date: "+fcd); ZonedDateTime zone = ZonedDateTime.now(); System.out.println("The Present zone is "+zone.getZone()); } public static void main(String[] args) { Zone(); } }The output is as follows: $javac Example.java $java Example Present formatted Time and Date: 19:24:52 13-12-2018 The Present zone is Etc/UTC |
|