Created
January 8, 2020 08:52
-
-
Save keepingcoding/faf1df73d440c7955104dcda0db0e761 to your computer and use it in GitHub Desktop.
日期工具类
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.text.SimpleDateFormat; | |
import java.time.LocalDateTime; | |
import java.time.ZoneId; | |
import java.time.ZoneOffset; | |
import java.util.Date; | |
public class DateUtils { | |
private DateUtils() { | |
} | |
/** | |
* 获取当前时间戳,精确到秒 | |
* | |
* @return | |
*/ | |
public static long getCurrSecond() { | |
LocalDateTime now = LocalDateTime.now(); | |
return now.toEpochSecond(ZoneOffset.of("+8")); | |
} | |
/** | |
* 获取当前时间戳,精确到毫秒 | |
* | |
* @return | |
*/ | |
public static long getCurrMillisecond() { | |
//LocalDateTime now = LocalDateTime.now(); | |
//return now.toInstant(ZoneOffset.of("+8")).toEpochMilli(); | |
return System.currentTimeMillis(); | |
} | |
/** | |
* 格式化日期 | |
* | |
* @param date 接受时间戳和 Date | |
* @param pattern 格式 | |
* @return | |
*/ | |
public static String formDate(Object date, String pattern) { | |
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); | |
return simpleDateFormat.format(date); | |
} | |
/** | |
* 获取指定日期的0分0秒 | |
* | |
* @param date | |
* @return | |
*/ | |
public static Date get0minAnd0SecDate(Date date) { | |
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); | |
localDateTime = localDateTime.withMinute(0).withSecond(0).withNano(0); | |
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