1、取当前时间戳
Long millisecond = Instant.now().toEpochMilli();
Long second = Instant.now().getEpochSecond();
2、将Long类型的时间戳转成字符串
public static String timeToString(Long time){
Assert.notNull(time, "time is null");
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time),ZoneId.systemDefault()));
}
3、时间字符串转成Long类型的时间戳
public static Long timeToLong(String time) {
Assert.notNull(time, "time is null");
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", ftf);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
4、时间转换Utils
public static String timeToString(Long time, String format){
Assert.notNull(time, "time is null");
DateTimeFormatter formatString = DateTimeFormatter.ofPattern(format);
return formatString.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
}
public static Long timeToLong(String time) {
Assert.notNull(time, "time is null");
DateTimeFormatter formatString = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", formatString);
return LocalDateTime.from(parse).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public static String localDateFormat(LocalDate localDate, String format){
Assert.notNull(localDate, "time is null");
String localDateStr = DateTranslate.timeToString(localDate2Second(localDate), format);
return localDateStr;
}
public static String localDateTimeFormat(LocalDateTime localDateTime, String format){
Assert.notNull(localDateTime, "time is null");
String localDateTimeStr = DateTranslate.timeToString(localDateTime2Second(localDateTime), format);
return localDateTimeStr;
}
public static LocalDate firstDayOfThisMonth() {
LocalDate today = LocalDate.now();
return today.with(TemporalAdjusters.firstDayOfMonth());
}
public static LocalDate firstDayOfLastMonth() {
LocalDate today = LocalDate.now();
today = today.minusMonths(1);
return today.with(TemporalAdjusters.firstDayOfMonth());
}
public static LocalDate dayOfThisMonth(int n) {
LocalDate today = LocalDate.now();
return today.withDayOfMonth(n);
}
public static LocalDate lastDayOfThisMonth() {
LocalDate today = LocalDate.now();
return today.with(TemporalAdjusters.lastDayOfMonth());
}
public static LocalDateTime firstDayOfWeek(Long date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
return localDateTime.with(DayOfWeek.MONDAY);
}
public static LocalDateTime firstDayOfLastWeek(Long date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
LocalDateTime localDateTime1 = localDateTime.plusDays(-7);
return localDateTime1.with(DayOfWeek.MONDAY);
}
public static LocalDateTime lastDay(Long date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault());
LocalDateTime localDateTime1 = localDateTime.plusDays(-1);
return localDateTime1;
}
public static LocalDateTime startOfThisMonth() {
return LocalDateTime.of(firstDayOfThisMonth(), LocalTime.MIN);
}
public static LocalDateTime endOfThisMonth() {
return LocalDateTime.of(lastDayOfThisMonth(), LocalTime.MAX);
}
public static Long localDate2Second(LocalDate localDate) {
return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
}
public static Long localDateTime2Second(LocalDateTime localDateTime) {
return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
}
java 8 Localdate常用API
====================================正确答==================================
getYear() int 获取当前日期的年份
getMonth() Month 获取当前日期的月份对象
getMonthValue() int 获取当前日期是第几月
getDayOfWeek() DayOfWeek 表示该对象表示的日期是星期几
getDayOfMonth() int 表示该对象表示的日期是这个月第几天
getDayOfYear() int 表示该对象表示的日期是今年第几天
withYear(int year) LocalDate 修改当前对象的年份
withMonth(int month) LocalDate 修改当前对象的月份
withDayOfMonth(int dayOfMonth) LocalDate 修改当前对象在当月的日期
isLeapYear() boolean 是否是闰年
lengthOfMonth() int 这个月有多少天
lengthOfYear() int 该对象表示的年份有多少天(365或者366)
plusYears(long yearsToAdd) LocalDate 当前对象增加指定的年份数
plusMonths(long monthsToAdd) LocalDate 当前对象增加指定的月份数
plusWeeks(long weeksToAdd) LocalDate 当前对象增加指定的周数
plusDays(long daysToAdd) LocalDate 当前对象增加指定的天数
minusYears(long yearsToSubtract) LocalDate 当前对象减去指定的年数
minusMonths(long monthsToSubtract) LocalDate 当前对象减去注定的月数
minusWeeks(long weeksToSubtract) LocalDate 当前对象减去指定的周数
minusDays(long daysToSubtract) LocalDate 当前对象减去指定的天数
compareTo(ChronoLocalDate other) int 比较当前对象和other对象在时间上的大小,返回值如果为正,则当前对象时间较晚,
isBefore(ChronoLocalDate other) boolean 比较当前对象日期是否在other对象日期之前
isAfter(ChronoLocalDate other) boolean 比较当前对象日期是否在other对象日期之后
isEqual(ChronoLocalDate other) boolean 比较两个日期对象是否相等