Skip to content

Instantly share code, notes, and snippets.

@zerosum
Created March 10, 2015 07:33
Show Gist options
  • Select an option

  • Save zerosum/6cf764b2026811491cb0 to your computer and use it in GitHub Desktop.

Select an option

Save zerosum/6cf764b2026811491cb0 to your computer and use it in GitHub Desktop.
LocalDateTime <-> Date
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.function.Function;
import java.util.function.UnaryOperator;
public class DateWrapper {
private LocalDateTime localDateTime;
public DateWrapper(Date date) {
this.localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault());
}
public DateWrapper(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
public DateWrapper map(UnaryOperator<LocalDateTime> f) {
this.localDateTime = f.apply(localDateTime);
return this;
}
public Boolean is(Function<LocalDateTime, Boolean> f) {
return f.apply(localDateTime);
}
public LocalDateTime toLocalDateTime() {
return localDateTime;
}
public Date toDate() {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment