java8处理时间

本文介绍如何使用 Java 8 中的新时间类 LocalDate 和 LocalDateTime 进行日期格式化、日期加减、日期间隔计算等操作,简化传统日期处理的复杂度。

新的时间类(LocalDate、LocalDateTime)

java8 之前我们处理时间 大多会涉及到这几个类Date、SimpleDateFormat、Calendar ,这种处理方式复杂、存在线程隐患、国际化困难、日期加减等处理麻烦等等。
现在有了 LocalDate、LocalDateTime、DateTimeFormatter 生活就变得简单了

格式化及区域定义

设置格式化模板

private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSS");
**设置日期时区常量**

```java
public static final ZoneId CHINA_ZONE_ID = ZoneId.systemDefault();

Date格式化为DateTime

@Test
public void dateToDateTime(){
    Date date = new Date();
    LocalDateTime dateTime = date.toInstant().atZone(CHINA_ZONE_ID).toLocalDateTime();
    System.out.println(dateTime);
}


LocalDate/LocalDateTime转Date

/**LocalDate/LocalDateTime转Date**/
@Test
public void toDate(){
  // LocalDate
  LocalDate localDate = LocalDate.now();
  Date d1 = Date.from(localDate.atStartOfDay(CHINA_ZONE_ID).toInstant());
  System.out.println(d1);

  // LocalDateTime
  LocalDateTime localDateTime = LocalDateTime.now();
  Date d2 = Date.from(localDateTime.atZone(CHINA_ZONE_ID).toInstant());
  System.out.println(d2);
}

日期格式化

/**日期格式化**/
@Test
public void formatDate(){        
  System.out.println(LocalDateTime.now().format(DATE_TIME_FORMATTER));
}

日期加减

/**日期加减**/
@Test
public void plusDay(){
    LocalDateTime dateTime = LocalDateTime.now(CHINA_ZONE_ID);
    //天
    dateTime=dateTime.plusDays(1);
    //时
    dateTime=dateTime.plusHours(-1);
    //分钟
    dateTime=dateTime.plusMinutes(30);
    System.out.println(dateTime.format(DATE_TIME_FORMATTER));
}

日期时间间隔

/**日期时间间隔**/
@Test
public void betweenDay(){
    // LocalDateTime
    LocalDateTime startDate = LocalDateTime.of(2019,07,01,12,12,22);
    LocalDateTime endDate = LocalDateTime.of(2019,07,03,12,12,22);
    Long withSecond =  endDate.atZone(CHINA_ZONE_ID).toEpochSecond() - startDate.atZone(CHINA_ZONE_ID).toEpochSecond();
    System.out.println(withSecond/60/60/24);

    // LocalDate
    LocalDate startDate2 = LocalDate.of(2019,07,01);
    LocalDate endDate2 = LocalDate.of(2019,07,03);
    Long withSecond2 =  endDate2.toEpochDay() - startDate2.toEpochDay();
    System.out.println(withSecond2);
}

第一天and最后一天

/**第一天and最后一天**/
@Test
public void theLastDay(){
    // 当月第一天
    LocalDateTime dateTime = LocalDateTime.of(2019,07,03,12,12,22);
    dateTime = dateTime.with(TemporalAdjusters.firstDayOfMonth());
    System.out.println(dateTime);
    // 当月最后一天
    dateTime = dateTime.with(TemporalAdjusters.lastDayOfMonth());
    System.out.println(dateTime);

    //当月的第几天
    dateTime = LocalDateTime.now();
    int dayOfMonth = dateTime.getDayOfMonth();
    System.out.println(dayOfMonth);
    // 当前周的第几天
    int dayOfWeek = dateTime.getDayOfWeek().getValue();
    System.out.println(dayOfWeek);
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值