补充:Java 中时间处理

1.常用时间类之间的转换

在这里插入图片描述

(1)util.Date -> sql.Date / TimeStamp

先转成Long类型时间戳,再转sql.Date / TimeStamp

 System.out.println(new Date());  // Thu Jul 14 14:53:40 CST 2022
 System.out.println(new Date().getTime());  // 1657781804545
System.out.println(new java.sql.Date(new Date().getTime()));   // 2022-07-14
System.out.println(new java.sql.Timestamp(new Date().getTime()));  // 2022-07-14 14:58:22.067

(2)sql.Date / TimeStamp -> util.Date

System.out.println(new Date(sqlDate.getTime()));
System.out.println(new Date(timeStamp.getTime()));

(3)LocalDate / LocalDateTime -> sql.Date / TimeStamp

System.out.println(java.sql.Date.valueOf(LocalDate.now()));  // 2022-07-14
System.out.println(java.sql.Timestamp.valueOf(LocalDateTime.now()));  // 2022-07-14 15:03:54.624

(4)sql.Date / TimeStamp -> LocalDate / LocalDateTime

LocalDateTime localDateTime1 = Timestamp.valueOf(LocalDateTime.now()).toLocalDateTime();
LocalDate localDate = java.sql.Date.valueOf(LocalDate.now()).toLocalDate();

(5)String -> LocalDate / LocalDateTime

String s1 = "2022-07-14 15:06:00";
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(LocalDateTime.from(formatter1.parse(s1)));   // 2022-07-14T15:06

String s2 = "2022-07-14";
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.from(formatter2.parse(s2)));   // 2022-07-14

(6)LocalDate / LocalDateTime -> String

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = LocalDateTime.now().format(formatter);
System.out.println(format);

(7)LocalDateTime -> LocalDate

LocalDate localDate = LocalDateTime.now().toLocalDate();

(8)LocalDate -> LocalDateTime

LocalDateTime localDateTime = LocalDate.now().atTime(8,20,33);

2.LocalDate / LocalDateTime

(1)指定日期初始化

LocalDateTime of = LocalDateTime.of(2022, 12, 5, 0, 0, 0, 0);

(2)指定时分秒纳秒

LocalDateTime with = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
System.out.println(with);

(3)获取当周周一和周日

LocalDate monday = LocalDate.now().with(DayOfWeek.MONDAY);
LocalDate sunday = LocalDate.now().with(DayOfWeek.SUNDAY);
System.out.println(monday);
System.out.println(sunday);

(4)获取当月月初和月末

LocalDate first = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
LocalDate last = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
System.out.println(first);
System.out.println(last);

(5)获取当年年初和年末

LocalDate first = LocalDate.now().with(TemporalAdjusters.firstDayOfYear());
LocalDate last= LocalDate.now().with(TemporalAdjusters.lastDayOfYear());
System.out.println(first);
System.out.println(last);

(6)时间比较

LocalDate first = LocalDate.now().with(TemporalAdjusters.firstDayOfYear());
LocalDate last = LocalDate.now().with(TemporalAdjusters.lastDayOfYear());
System.out.println(first.isBefore(last));
System.out.println(first.isAfter(last));

(7)时间差值

LocalDateTime first = LocalDateTime.now().with(TemporalAdjusters.firstDayOfYear());
LocalDateTime last = LocalDateTime.now().with(TemporalAdjusters.lastDayOfYear());
long l = Duration.between(first, last).toDays();
System.out.println(l);

(8)时区转换

ZonedDateTime dateTimeUtc = ZonedDateTime.parse(timeString, DateTimeFormatter.ISO_DATE_TIME);
ZonedDateTime dateTimeChina = dateTimeUtc.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));
dateTimeChina.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

3.YearMonth

YearMonth now = YearMonth.now();
System.out.println(now);
YearMonth parse = YearMonth.parse("2022-11");
System.out.println(parse.getYear());
System.out.println(parse.getMonthValue());
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值