Converting Java 8 Time, Date, and DateTime (java.util.Date)

Converting Java 8 Time, Date, and DateTime to and from java.util.Date is not that straightforward as you first might think. The new Java8 java.time package is a really great improvement, but current ORMs, databases, etc. do not fully support the new data types yet so we need means to transform all time instances to good old java.util.Date. I wrote some helper methods that are available at DateAndTimeConverter.

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

public class DateAndTimeConverter {
    public static Instant convertToInstant(Date ts) {
        Instant i = Instant.ofEpochMilli(ts.getTime());
        
        return i;
    }
    public static Date convertToDate(Instant i) {
        Date d = Date.from(i);
        
        return d;
    }
    /*
     * Convert java.util.Date to java.time.LocalDateTime that is a
     * date-time without a time-zone in the ISO-8601 calendar system,
     * such as 2007-12-03T10:15:30. It does not store or represent a time-zone!
     */
    public static LocalDateTime convertToLocalDateTime(Date ts) {
        // instant is offset from epoch
        Instant i = Instant.ofEpochMilli(ts.getTime());
        LocalDateTime ldt = LocalDateTime.ofInstant(i, ZoneId.systemDefault());
        
        return ldt;
    }
    /*
     * Convert java.time.LocalDateTime to java.util.Date
     */
    public static Date convertToDate(LocalDateTime ldt) {
        Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
        Date d = Date.from(instant);
        
        return d;
    }
    /*
     * Convert java.util.Date to java.time.LocalDate that is a
     * date that represents a date without a time zone, such as 20-12-2013. 
     */
    public static LocalDate convertToLocalDate(Date ts) {
        Instant i = Instant.ofEpochMilli(ts.getTime());
        LocalDate ld = LocalDateTime.ofInstant(i, ZoneId.systemDefault()).toLocalDate();
        
        return ld;
    }
    /*
     * Convert java.time.LocalDate to java.util.Date
     */
    public static Date convertToDate(LocalDate ld) {
        Instant instant = ld.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        Date d = Date.from(instant);
        
        return d;
    }
    /*
     * Convert java.util.Date to java.time.LocalTime that is a
     * time that represents a time without a time zone, such as 01:23:455.67
     */
    public static LocalTime convertToLocalTime(Date ts) {
        Instant i = Instant.ofEpochMilli(ts.getTime());
        LocalTime lt = LocalDateTime.ofInstant(i, ZoneId.systemDefault()).toLocalTime();
        
        return lt;
    }
    /*
     * Convert java.time.LocalTime to java.util.Date
     */
    public static Date convertToDate(LocalTime lt) {
        LocalDate ld = LocalDate.now();
        Instant instant = lt.atDate(LocalDate.of(ld.getYear(), ld.getMonth(), ld.getDayOfMonth())).atZone(ZoneId.systemDefault()).toInstant();
        Date d = Date.from(instant);
        
        return d;
    }
}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.