Last active
December 9, 2016 10:27
-
-
Save preslavrachev/4823ff51d3add56c830c to your computer and use it in GitHub Desktop.
A simple utility class for converting Date to Java 8's LocalDate and vice versa. Via: http://stackoverflow.com/questions/22929237/convert-java-time-localdate-into-java-util-date-type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DateUtil { | |
public static Date asDate(LocalDate localDate) { | |
return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()); | |
} | |
public static Date asDate(LocalDateTime localDateTime) { | |
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()); | |
} | |
public static LocalDate asLocalDate(Date date) { | |
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate(); | |
} | |
public static LocalDateTime asLocalDateTime(Date date) { | |
return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LocalDateTimeUtil { | |
public static long betweenInMinutes(LocalDateTime t1, LocalDateTime t2) { | |
return ChronoUnit.MINUTES.between(t1, t2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment