将指定时间格式化为LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse("2021-03-29 00:00:00", DateTimeFormatter.ofPattern(format));
获取当前时间的LocalDateTime
LocalDateTime now = LocalDateTime.now();
获取当前时间
String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
获取指定时间在这个月的第几天
int day = MonthDay.from(LocalDateTime.parse("2020-01-25 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).getDayOfMonth();
获取指定时间是星期几
int week = DayOfWeek.from(LocalDateTime.parse("2021-03-29 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).getValue();
获取指定时间是第几个月
int month = YearMonth.from(LocalDateTime.parse("2020-12-01 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))).getMonthValue();
将当前时间偏移指定时间(毫秒/秒/分钟/小时/天/月/年)(正数向以后偏移,负数向从前偏移)
LocalDateTime time1 = LocalDateTime.now().plusMinutes(1000);
LocalDateTime time2 = LocalDateTime.now().plusSeconds(60);
LocalDateTime time3 = LocalDateTime.now().plusMinutes(5);
LocalDateTime time4 = LocalDateTime.now().plusHours(5);
LocalDateTime time5 = LocalDateTime.now().plusDays(5);
LocalDateTime time6 = LocalDateTime.now().plusMonths(5);
LocalDateTime time7 = LocalDateTime.now().plusYears(1);
获取指定时间在那 一分钟/一小时/一天/一周/一个月/一年 的初始时间
/**
* 获取指定时间在那一分钟的初始时间。
*
* @param time 指定时间
* @param pattern 时间格式,例如 yyyy-MM-dd HH:mm:ss
* @return 初始时间
*/
public static String getFirstTimeOfMinute(String time, String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
return dateTimeFormatter.format(localDateTime.withSecond(0));
}
/**
* 获取指定时间在那一小时的初始时间。
*
* @param time 指定时间
* @param pattern 时间格式,例如 yyyy-MM-dd HH:mm:ss
* @return 初始时间
*/
public static String getFirstTimeOfHours(String time, String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
return dateTimeFormatter.format(localDateTime.withMinute(0).withSecond(0));
}
/**
* 获取指定时间在那一天的初始时间。
*
* @param time 指定时间
* @param pattern 时间格式,例如 yyyy-MM-dd HH:mm:ss
* @return 初始时间
*/
public static String getFirstTimeOfDay(String time, String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
return dateTimeFormatter.format(localDateTime.withHour(0).withMinute(0).withSecond(0));
}
/**
* 获取指定时间在那一周的初始时间。
*
* @param time 指定时间
* @param pattern 时间格式,例如 yyyy-MM-dd HH:mm:ss
* @return 初始时间
*/
public static String getFirstDayOfWeek(String time, String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
return dateTimeFormatter.format(localDateTime.with(DayOfWeek.MONDAY).withHour(0).withMinute(0).withSecond(0));
}
/**
* 获取指定时间在那一月的初始时间。
*
* @param time 指定时间
* @param pattern 时间格式,例如 yyyy-MM-dd HH:mm:ss
* @return 初始时间
*/
public static String getFirstDayOfMonth(String time, String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
return dateTimeFormatter.format(localDateTime.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0));
}
/**
* 获取指定时间在那一年的初始时间。
*
* @param time 指定时间
* @param pattern 时间格式,例如 yyyy-MM-dd HH:mm:ss
* @return 初始时间
*/
public static String getFirstDayOfYear(String time, String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
return dateTimeFormatter.format(localDateTime.withDayOfYear(1).withHour(0).withMinute(0).withSecond(0));
}
public static void main(String[] args) {
String minute = getFirstTimeOfMinute("2021-03-24 12:13:14", "yyyy-MM-dd HH:mm:ss");
String hours = getFirstTimeOfHours("2021-03-24 12:13:14", "yyyy-MM-dd HH:mm:ss");
String day = getFirstTimeOfDay("2021-03-24 12:13:14", "yyyy-MM-dd HH:mm:ss");
String week = getFirstDayOfWeek("2021-03-24 12:13:14", "yyyy-MM-dd HH:mm:ss");
String month = getFirstDayOfMonth("2021-03-24 12:13:14", "yyyy-MM-dd HH:mm:ss");
String year = getFirstDayOfYear("2021-03-24 12:13:14", "yyyy-MM-dd HH:mm:ss");
System.out.println(minute);
System.out.println(hours);
System.out.println(day);
System.out.println(week);
System.out.println(month);
System.out.println(year);
}
/**
* 结果
* 2021-03-24 12:13:00
* 2021-03-24 12:00:00
* 2021-03-24 00:00:00
* 2021-03-22 00:00:00
* 2021-03-01 00:00:00
* 2021-01-01 00:00:00
*/
获取两个时间相差多少毫秒
/**
* 获取两个时间的时间差,单位:毫秒。
*
* @param beginTime 开始时间
* @param endTime 结束时间
* @return 毫秒时间差
*/
public static Long getMinutesDiff(String beginTime, String endTime) {
long diff = timeToMilli(endTime) - timeToMilli(beginTime);
return TimeUnit.MILLISECONDS.toMillis(diff);
}
获取两个时间相差多少分钟
/**
* 获取两个时间的时间差,单位:分钟。
*
* @param beginTime 开始时间
* @param endTime 结束时间
* @param format 自定义时间格式,例如yyyy-MM-dd HH:mm
* @return 分钟时间差
*/
public static Long getMinutesDiff(String beginTime, String endTime, String format) {
LocalDateTime begin = LocalDateTime.parse(beginTime, DateTimeFormatter.ofPattern(format));
LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern(format));
return Math.abs(Duration.between(begin, end).toMinutes());
}
时间转毫秒值
/**
* 时间转毫秒值。
*
* @param time 时间
* @param format 自定义时间格式,例如yyyy-MM-dd HH:mm
* @return 毫秒值
*/
public static Long timeToMilli(String time, String format) {
return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(format))
.atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
}
毫秒值转时间
/**
* 毫秒值转时间。
*
* @param milliseconds 时间毫秒值
* @param format 自定义时间格式,例如yyyy-MM-dd HH:mm
* @return 时间
*/
public static String MilliTotTime(long milliseconds, String format) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(format));
}
获取指定时间前后,偏移指定月的时间
/**
* 获取指定时间前后,偏移指定月的时间。
*
* @param currentTime 指定时间
* @param num 指定几个月,正数为向以后偏移,负数反之
* @param format 时间格式,例如yyyy-MM-dd HH:mm:ss
* @return 偏移后的时间
*/
public static String getOffsetMonthsTime(String currentTime, Integer num, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDateTime time = LocalDateTime.parse(currentTime, dateTimeFormatter);
return time.plusMonths(num).format(dateTimeFormatter);
}
获取指定时间前后,偏移指定天的时间
/**
* 获取指定时间前后,偏移指定天的时间。
*
* @param currentTime 指定时间
* @param num 指定几天
* @param format 时间格式,例如yyyy-MM-dd HH:mm:ss
* @return 偏移后的时间
*/
public static String getOffsetHoursTime(String currentTime, Integer num, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDateTime time = LocalDateTime.parse(currentTime, dateTimeFormatter);
return time.plusDays(num).format(dateTimeFormatter);
}
获取偏移指定天后,两个时间的间隔毫秒数
/**
* 获取偏移指定天后,两个时间的间隔毫秒数。
*
* @param time 指定时间
* @param num 指定几天,正数为向以后偏移,负数反之
* @param format 时间格式,例如yyyy-MM-dd HH:mm:ss
* @return 偏移后,两个时间的时间差
*/
public static Long getOffsetDaysIntervalMilli(String time, Integer num, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDateTime time1 = LocalDateTime.parse(time, dateTimeFormatter);
LocalDateTime time2 = time1.plusDays(num);
return Math.abs(Duration.between(time1, time2).toMillis());
}
获取偏移指定天后,两个时间的间隔分钟数
/**
* 获取偏移指定天后,两个时间的间隔分钟数。
*
* @param time 指定时间
* @param num 指定几天,正数为向以后偏移,负数反之
* @param format 时间格式,例如yyyy-MM-dd HH:mm:ss
* @return 偏移后,两个时间的时间差
*/
public static Long getOffsetDaysIntervalMinutes(String time, Integer num, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDateTime time1 = LocalDateTime.parse(time, dateTimeFormatter);
LocalDateTime time2 = time1.plusDays(num);
return Math.abs(Duration.between(time1, time2).toMinutes());
}