In Java 8 added some new methods with Legacy java.util Date and Time classes to convert object of java.time classes to get advantage new java 8 Date and Time classes with minimal changes.
Points To Remember:
- java.util.Date and java.time.Instance classes are similar. The Date.from(Instant) and Date.toInstant() methods allow conversion between these classes. It represents instantaneous point of time in UTC which is independent of TimeZone as epoch-seconds (since 1970-01-01T00:00:00Z) plus nanoseconds.
- The java.time.ZonedDateTime class is the replacement for java.util.GregorianCalendar. The GregorianCalendar.from(ZonedDateTime) and GregorianCalendar.to(ZonedDateTime) methods faciliate conversions between these classes. It present time as follows:
- LocalDate : year, month and day
- LocalTime: hour, minute, second, nanoseconds
- ZoneId: time zone
- ZoneOffset: current offset from GMT
- The ZoneOffset class specifies only an offset from Greenwich/UTC. The ZoneId class specifies a time zone identifier and has access to the rules used each time zone.
- GeregorianCalendar with date as 1970-01-01 will use date component can be replaced with an instance of LocalDate.
- GeregorianCalendar with time as 00:00 will use time component can be replaced with an instance of LocalTime.
java.util.Date to an java.time.Instant
Date date=new Date();
Instant instant=date.toInstant();
An java.time.Instant to java.util.Date
Date date=Date.from(instant);
java.util.Calendar to an java.time.Instant
Calendar cal=Calendar.getInstance();
Instant instant=cal.toInstant();
java.util.GregorianCalendar instance to an java.time.ZoneDateTime
Calendar gCal=GregorianCalendar.getInstance();
ZoneDateTime zoneDateTime=ZoneDateTime.ofInstant(gCal.toInstant(),ZoneId.systemDefault());
java.time.ZoneDateTime instance to java.util.GregorianCalendar
GregorianCalendar gCal=GregorianCalendar.from(zoneDateTime);
java.util.TimeZone object to java.time.ZoneId
Calendar gCal1=GregorianCalendar.getInstance();
TimeZone tz = cal.getTimeZone();
ZoneId zoneID=tz.toZoneId();
int tzoffset = cal.get(Calendar.ZONE_OFFSET);
Example
In below example considered all the cases of Legacy to Java 8 object classes.
package com.date; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.TimeZone; public class Legacy2Java8DateTimeInteroprability { public static void main(String[] args) { SimpleDateFormat utilDateTimeFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); DateTimeFormatter java8DateTimeFormat=DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").withZone(ZoneId.systemDefault());; //Date to Instant Date date=new Date(); Instant inst = date.toInstant(); System.out.println("Initial Date :"+utilDateTimeFormat.format(date)); System.out.println("Converted Instant :"+java8DateTimeFormat.format(inst)); //Instant To Date Date newDate = Date.from(inst); System.out.println("Converted Date :"+utilDateTimeFormat.format(date)); //Calendar to ZoneDateTime Calendar cal = Calendar.getInstance(); ZonedDateTime zdt = ZonedDateTime.ofInstant(cal.toInstant(), ZoneId.systemDefault()); System.out.println("Calendar to ZoneDateTime :"+java8DateTimeFormat.format(zdt)); //Calendar to Instant Instant inst1 = cal.toInstant(); System.out.println("Calendar to Instant :"+java8DateTimeFormat.format(inst1)); //Geregorian calendar from Zone Date Time GregorianCalendar gcal = GregorianCalendar.from(zdt); System.out.println("Gregorian Calendar Date Time:"+utilDateTimeFormat.format(gcal.getTime())); ZonedDateTime zdt1 = gcal.toZonedDateTime(); System.out.println("Calendar to ZoneDateTime Date :"+java8DateTimeFormat.format(zdt1)); LocalDateTime ldt = zdt1.toLocalDateTime(); System.out.println("Local Date Time :"+java8DateTimeFormat.format(ldt)); LocalDate localDate = zdt1.toLocalDate(); System.out.println("Local Date :"+java8DateTimeFormat.format(ldt)); LocalTime localTime = zdt1.toLocalTime(); System.out.println("Local Time :"+java8DateTimeFormat.format(ldt)); //TimeZone to Zone Id Calendar gCal1=GregorianCalendar.getInstance(); TimeZone tz = cal.getTimeZone(); System.out.println("Time Zone :"+tz.getDisplayName()); ZoneId zoneID=tz.toZoneId(); System.out.println("Zone ID:"+zoneID.getId()); int tzoffset = cal.get(Calendar.ZONE_OFFSET); <span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>System.out.println("Zone Offset:"+tzoffset); } }
Output
Initial Date :2018/08/04 07:48:45
Converted Instant :2018/08/04 07:48:45
Converted Date :2018/08/04 07:48:45
Calendar to ZoneDateTime :2018/08/04 07:48:45
Calendar to Instant :2018/08/04 07:48:45
Gregorian Calendar Date Time:2018/08/04 07:48:45
Calendar to ZoneDateTime Date :2018/08/04 07:48:45
Local Date Time :2018/08/04 07:48:45
Local Date :2018/08/04 07:48:45
Local Time :2018/08/04 07:48:45
Time Zone :Pacific Standard Time
Zone ID:America/Los_Angeles
Zone Offset:-28800000
You must log in to post a comment.