Skip to content

Instantly share code, notes, and snippets.

@preslavrachev
Last active December 9, 2016 10:27
Show Gist options
  • Save preslavrachev/4823ff51d3add56c830c to your computer and use it in GitHub Desktop.
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
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();
}
}
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