LocalDate 年月日
LocalTime 时分秒
LocalDateTime 年月日时分秒
获取当前时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
指定时间创建对象
LocalDate localDate = LocalDate.of(2018, 1, 13);
LocalTime localTime = LocalTime.of(9, 43, 20);
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 13, 9, 43, 20);
日期时间的加减
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime plusYearsResult = localDateTime.plusYears(2L);
// 以下方法的参数都是long型,返回值都是LocalDateTime
// 加 plusYears、plusMonths、plusDays、plusHours、plusMinutes、plusSeconds
// 减 minusYears、minusMonths、minusDays、minusHours、minusMinutes、minusSeconds
将年、月、日等修改为指定的值,并返回新的日期(时间)对象
LocalDate localDate = LocalDate.now();
//当前时间基础上,指定本年当中的第几天,取值范围为1-365,366
LocalDate withDayOfYearResult = localDate.withDayOfYear(200);
//当前时间基础上,指定本月当中的第几天,取值范围为1-29,30,31
LocalDate withDayOfMonthResult = localDate.withDayOfMonth(5);
//当前时间基础上,直接指定年份
LocalDate withYearResult = localDate.withYear(2017);
//当前时间基础上,直接指定月份
LocalDate withMonthResult = localDate.withMonth(5);
获取日期的年月日周时分秒
LocalDateTime localDateTime = LocalDateTime.now();
int dayOfYear = localDateTime.getDayOfYear();// 本年当中第几天
int dayOfMonth = localDateTime.getDayOfMonth();// 本月当中第几天
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek(); // 本周中星期几 英文 dayOfWeek.getValue();// 本周中星期几 数字
int year = localDateTime.getYear();
Month month = localDateTime.getMonth();
int day = localDateTime.getDayOfMonth();
int hour = localDateTime.getHour();
int minute = localDateTime.getMinute();
int second = localDateTime.getSecond();
判断两个时间点的前后
LocalDate localDate1 = LocalDate.of(2017, 8, 8);
LocalDate localDate2 = LocalDate.of(2018, 8, 8);
boolean date1IsBeforeDate2 = localDate1.isBefore(localDate2);
计算日期间隔年月日
Duration:用于计算两个“时间”间隔
Period:用于计算两个“日期”间隔
LocalDate date1 = LocalDate.of(2018, 2, 13);
LocalDate date2 = LocalDate.of(2017, 3, 12);
//内部是用date2-date1,所以得到的结果是负数
Period period = Period.between(date1, date2);
System.out.println("相差年数 : " + period.getYears());
System.out.println("相差月数 : " + period.getMonths());
System.out.println("相差日数 : " + period.getDays());
//还可以这样获取相差的年月日
long years = period.get(ChronoUnit.YEARS);
long months = period.get(ChronoUnit.MONTHS);
long days = period.get(ChronoUnit.DAYS);
天 :" + duration.toDays() + "\n"
时 :" + duration.toHours() + "\n"
分 :" + duration.toMinutes() + "\n"
毫秒 :" + duration.toMillis() + "\n"
纳秒 :" + duration.toNanos() + "\n"
运行耗时
Duration duration = Duration.between(ins1, ins2);
System.out.println("程序运行耗时为 : " + duration.toMillis() + "毫秒");
时间日期格式化
LocalDateTime date1 = LocalDateTime.now();
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String date2Str = formatter2.format(date1);
将时间字符串形式转化为日期对象
String datetime = "2018-01-13 21:27:30";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse(datetime, dtf);
时间日期对象转为格式化日期对象
LocalDateTime formatedDateTime = LocalDateTime.parse(datetime, dtf);
long毫秒值转换为日期
DateTimeFormatter df= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String longToDateTime = df.format(LocalDateTime.ofInstant(
Instant.ofEpochMilli(System.currentTimeMillis()),ZoneId.of("Asia/Shanghai")));