前言:时间操作在开发中经常被使用到,最近项目是用Java8开发,因此总结一下时间操作常用方法。
// 当前日期:2019-03-16
LocalDate date = LocalDate.now();
// 当前时间:22:13:03.450
LocalTime time = LocalTime.now();
// 当前日期和时间:2019-03-16T22:13:03.450
LocalDateTime dateTime = LocalDateTime.now();
LocalDate now = LocalDate.now();
// 本月第一天: 2019-03-01
LocalDate with = now.with(TemporalAdjusters.firstDayOfMonth());
// 本月最后一天:2019-03-31
LocalDate with1 = now.with(TemporalAdjusters.lastDayOfMonth());
LocalDate now = LocalDate.now();
// 下一个月的第一天:2019-04-01
LocalDate with2 = now.with(TemporalAdjusters.firstDayOfNextMonth());
// 下一年的第一天:2020-01-01
LocalDate with3 = now.with(TemporalAdjusters.firstDayOfNextYear());
LocalDate now = LocalDate.now();
// 获取当前年的第一天
LocalDate with4 = now.with(TemporalAdjusters.firstDayOfYear());
// 获取当前年的第一个周一所在的日期:2019-01-07
LocalDate.parse(with4.toString()).with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
LocalDate now = LocalDate.now();
// 当前时间的后两天: 2019-03-18
LocalDate date1 = now.plusDays(2);
// 当前时间的后两月:2019-05-16
LocalDate date2 = now.plusMonths(2);
// 实例化一个时间
LocalDate of = LocalDate.of(2014, 11, 07);
// 相距多少年
long until = now.until(of, ChronoUnit.YEARS);
// 相距多少月
long until2 = now.until(of, ChronoUnit.MONTHS);
// 相距多少天
long until3 = now.until(of, ChronoUnit.DAYS);
LocalDate nowDate = LocalDate.of(2019, 03, 17);
LocalDate birthDayTime = LocalDate.of(1995, 03, 16);
MonthDay birthday = MonthDay.of(birthDayTime.getMonth(), birthDayTime.getDayOfMonth());
MonthDay today = MonthDay.from(nowDate);
if(birthday.equals(today)){
System.out.println("今天是我的生日");
}else{
System.out.println("今天不是我的生日");
}
LocalDate today = LocalDate.now();
LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
LocalDate today = LocalDate.now();
LocalDate preYear = today.minus(1, ChronoUnit.YEARS);
LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
LocalDate today = LocalDate.now();
if (today.isLeapYear()) {
System.out.println("今年是闰年!");
} else {
System.out.println("今年不是闰年!");
}
YearMonth currentYearMonth = YearMonth.now();
// 月份有多少天
currentYearMonth.lengthOfMonth();
// 信用卡到期日期
YearMonth creditCardExpiry = YearMonth.of(2018, Month.FEBRUARY);
// date转为LocalTime
Date from = Date.from(instant);
Instant instant2 = from.toInstant();
instant2.atZone(zone).toLocalTime();
// 将LocalDateTime转为date
ZoneId zone = ZoneId.systemDefault();
Instant instant = now.atZone(zone).toInstant();
Date from = Date.from(instant);
说明:目前项目中就使用了这些,如果你有其他常用的操作,请留言,持续补充