Created
January 2, 2019 08:18
-
-
Save v5tech/3b8bafac59e1d780b7864960695f70f6 to your computer and use it in GitHub Desktop.
java 8日期转化
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
import java.time.Instant; | |
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.util.Date; | |
public class DateUtils { | |
public static Date asDate(LocalDate localDate) { | |
return Date.from(localDate.atStartOfDay(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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment