LocalDateTime.now()

获取当前日期 + 时间,返回值为LocalDateTime

LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime);
//2021-06-16T10:58:11.582

时间不对?可能没设置对时区

//获取可用时区
System.out.println(ZoneId.getAvailableZoneIds());
//设置上海时区
LocalDateTime now = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));

DateTimeFormatter

格式化,大写的 M 代表月,小写的 m 代表分钟,返回值为String

LocalDateTime now = LocalDateTime.now();
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
System.out.println(formatter.format(now));
//由于 date 没有小时分钟,所以不能用上面的 DateTimeFormatter
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(formatter2.format(date));
//2021-06-16 10:58:59
//2021-06-16

get方法

获取一些特定的值

LocalDateTime now = LocalDateTime.now();
System.out.println(now.getDayOfWeek());
System.out.println(now.getYear());
//WEDNESDAY
//2021

java.util.date 与 localdate转换

都是先转成 Instant 再进行转换

//localdatetime -> date
LocalDateTime localDateTime = LocalDateTime.now();
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
//date -> localdatetime
Date date = new Date();
Instant instant = date.toInstant();
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();

添加、减少时间,返回值为当前调用该方法的原类型,例如 LocalDateTime.plus 返回值为 LocalDateTime

LocalDateTime now = LocalDateTime.now();
//增加一天
System.out.println(now.plusDays(1));
System.out.println(now);
//减少一天
System.out.println(now.minusDays(1));
//2021-06-17T11:28:11.436
//2021-06-16T11:28:11.436
//2021-06-15T11:28:11.436

获取指定日期,返回值同上

LocalDateTime point = LocalDateTime.of(2020,01,01,00,00,01);
System.out.println(point);
//2020-01-01T00:00:01

修改某些特定的值,可以混合使用,返回值同上

LocalDateTime now = LocalDateTime.now();
//当前年的第一天
System.out.println(now.withDayOfYear(1));
//2021-01-01T13:58:44.731

TemporalAdjusters

dayOfWeekInMonth 获取调用该方法的LocalDate同月的第 ordinal 周的 周几的日期 int ordinal 第几周
DayOfWeek dayOfWeek 周几 firstDayOfMonth 获取调用该方法的LocalDate同月的第一天的日期 firstDayOfNextMonth 获取调用该方法的LocalDate下一个月的第一天的日期 firstDayOfYear 获取调用该方法的LocalDate同年的第一天的日期 firstDayOfNextYear 获取调用该方法的LocalDate下一年的第一天的日期 firstInMonth 获取调用该方法的LocalDate同月的第一周的 周几 的日期 DayOfWeek dayOfWeek 周几 获取下一周的周几 DayOfWeek dayOfWeek 周几 nextOrSame 获取这一周或者下一周的周几(当当前小于需要查询的周几时,返回这周日期,否则为下周日期,例如,今天为星期三,查询周五,则返回这周周五,查询周二,返回下周周二) DayOfWeek dayOfWeek 周几 previous 获取上一周的周几 DayOfWeek dayOfWeek 周几 previousOrSame 获取这一周或者上一周的周几 DayOfWeek dayOfWeek 周几
LocalDate now = LocalDate.now();
//今天是2021-06-16、、WEDNESDAY
System.out.println("今天是"+now+"、、"+now.getDayOfWeek());
//获取第 2 周的周一	2021-06-14
System.out.println(now.with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.MONDAY)));
//获取本月的第一天 		2021-06-01
System.out.println(now.with(TemporalAdjusters.firstDayOfMonth()));
//获取下个月的第一天		2021-07-01
System.out.println(now.with(TemporalAdjusters.firstDayOfNextMonth()));
//获取今年的第一天		2021-01-01
System.out.println(now.with(TemporalAdjusters.firstDayOfYear()));
//获取明年的第一天		2022-01-01
System.out.println(now.with(TemporalAdjusters.firstDayOfNextYear()));
//获取本月的第一个周一	2021-06-07
System.out.println(now.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)));
//获取本月的最后一天		2021-06-30
System.out.println(now.with(TemporalAdjusters.lastDayOfMonth()));
//获取今年的最后一天		2021-12-31
System.out.println(now.with(TemporalAdjusters.lastDayOfYear()));
//获取本月的最后一个周一	2021-06-28
System.out.println(now.with(TemporalAdjusters.lastInMonth(DayOfWeek.MONDAY)));
//获取下一个周三		2021-06-23
System.out.println(now.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY)));
//获取还未过完的下一次周三	2021-06-16
System.out.println(now.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY)));
//获取上一个周三		2021-06-09
System.out.println(now.with(TemporalAdjusters.previous(DayOfWeek.WEDNESDAY)));
//获取上一个或者已经过了的周三(含未过完)	2021-06-16
System.out.println(now.with(TemporalAdjusters.previousOrSame(DayOfWeek.WEDNESDAY)));

时间戳 Instant

  • Instant.now 获取UTC时间
  • Instant now = Instant.now();
    System.out.println(now);
    //2021-06-16T07:19:31.618Z
    
  • UTC时间和中国时间差8小时,获取中国时间
  • Instant now = Instant.now();
    System.out.println(now.atOffset(ZoneOffset.ofHours(8)));
    //2021-06-16T15:25:35.905+08:00
    
  • 获取时间戳
  • Instant now = Instant.now();
    System.out.println(System.currentTimeMillis());
    System.out.println(now.toEpochMilli());
    //1623828336034
    //1623828335905
    
    Instant now = Instant.now();
    System.out.println(now.getEpochSecond());
    //1623828335
    
  • 两个时间点的 时间
  • Duration.between(Instant, Instant);

    duration包含多个转换,toMills 转成毫秒,可以获得时间戳,getSeconds转成秒数,等等。

    //获取当前时间
    LocalDateTime now = LocalDateTime.now();
    //时间纪年 1970-01-01 00:00:00.0
    LocalDateTime start = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0);
    //将时间转换为Instant
    Instant instant = now.toInstant(ZoneOffset.UTC);
    Instant startInstant = start.toInstant(ZoneOffset.UTC);
    //获取两个时间间隔
    //java.time.Duration
    Duration duration = Duration.between(startInstant, instant);
    //时间戳
    System.out.println(duration.toMillis());
    System.out.println(instant.toEpochMilli());
    
  • 两个时间点的 日期
  • Period.between(LocalDate, LocalDate);

    period包含多个转换方法,getDays转成天数,等等。

    LocalDateTime now = LocalDateTime.now();
    LocalDateTime start = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0);
    //转换为localDate
    LocalDate localDate = now.toLocalDate();
    LocalDate startDate = start.toLocalDate();
    Period period = Period.between(startDate, localDate);
    System.out.println(period.getYears());