LocalDate/LocalDateTime/LocalTime/Date

package com.company;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author xiesongzhuang1
 * @Description TODO
 * @createTime 2021年08月20日
 */
public class test {
    public static final LocalTime ON_HOUR_TIME = LocalTime.of(8, 0);
    public static final LocalTime OFF_HOUR_TIME = LocalTime.of(20, 0);
 public static  void  main(String[] args){
     //Date 转LocalDate
     Date date = new Date();
     LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
     //Date 转LocalDateTime
     LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
     //LocalDateTime转String
     String localDate3=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime().format(DateTimeFormatter.ofPattern("HH:mm"));
     System.out.println("date--"+date);//date--Mon Aug 23 16:23:48 CST 2021
     System.out.println("LocalDate--"+localDate);//LocalDate--2021-08-23
     System.out.println("localDateTime--"+localDateTime);//localDateTime--2021-08-23T16:40:12.962
     System.out.println("LocalDate3--"+localDate3);//LocalDate2--16:37
     //LocalDateTime 转Date
     LocalDateTime localDateTime2 = LocalDateTime.now();
     Date date2 = Date.from(localDateTime.toInstant(ZoneOffset.UTC));
     System.out.println("localDateTime2--"+localDateTime2);//localDateTime2--2021-08-23T20:33:15.489
     System.out.println("date2--"+date2);//date2--Tue Aug 24 04:33:15 CST 2021
     //LocalDateTime 转Date  标准版
     Date date3= Date.from(localDateTime2.atZone(ZoneId.systemDefault()).toInstant());
     System.out.println("date3-----"+date3);//date3-----Mon Aug 23 20:33:15 CST 2021
    
    //LocalTime
     System.out.println(ON_HOUR_TIME);//08:00
    //LocalTime到LocalDateTime
     LocalDateTime startLocalDate = localDate.atTime(ON_HOUR_TIME);
     System.out.println(startLocalDate);//2021-08-23T08:00

     LocalDateTime endLocalDate=localDate.atTime(OFF_HOUR_TIME);
     System.out.println("endLocalDate---"+endLocalDate);//endLocalDate---2021-08-23T20:00
     
     LocalDateTime minTime = localDate.atTime(LocalTime.MIN);
     LocalDateTime maxTime = localDate.atTime(LocalTime.MAX);

     System.out.println(minTime);//2021-08-23T00:00
     System.out.println(maxTime);//2021-08-23T23:59:59.999999999
    // 计算两个时间的差值LocalDateTime
     Duration dur = Duration.between(startLocalDate, endLocalDate);

     System.out.println("dur-----"+dur);//dur-----PT12H




 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值