java8 时间 LocalDateTime常用方法

方法说明
获取月份getMonthValue()(数字)12
获取月份getMonth()(英文)DECEMBER
获取小时getHour()18
获取分钟getMinute()57
获取当前时间月份的第几天getDayOfMonth()4
获取当前周的第几天getDayOfWeek()TUESDAY
获取当前时间在该年属于第几天getDayOfYear()338
获取时间toLocalTime()18:57:14.098
获取日期toLocalDate()2018-12-04
字符串表示toString()2018-12-04T18:57:14.098
A在B之后A.isAfter(B)true/false
A在B之前A.isBeforer(B)true/false

LocalDateTime time = LocalDateTime.now();

 time.toString(); //字符串表示-----------------------------------2018-12-04T18:57:14.098
 time.toLocalTime(); //获取时间(LocalTime)------------------------------------18:57:14.098
 time.toLocalDate(); //获取日期(LocalDate)--------------------------------------2018-12-04
 time.getDayOfMonth(); //获取当前时间月份的第几天--------------------------------4
 time.getDayOfWeek();  //获取当前周的第几天----------------------------------TUESDAY
 time.getDayOfYear();  //获取当前时间在该年属于第几天-----------------------------338
 time.getHour();//获取小时--------------------------------------------18
 time.getMinute();//获取分钟-------------------------------------------57
 time.getMonthValue();//获取月份(数字)-----------------------12
 time.getMonth();//获取月份(英语)--------------------DECEMBER

日期字符格式化

//localDdate格式为字符串
    DateTimeFormatter dfr = DateTimeFormatter.ofPattern("YYYY/MM/dd HH:mm:ss");
    System.out.println(time.format(df));
    
//字符串格式为localDdate
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    System.out.println(LocalDate.parse("2018-10-10", df)));
    
//localDdateTime格式为字符串
 DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 time.format(df)----------------------------2018-12-04 18:57:14
 
 //字符型格式化为LocalDateTime
 DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:");
 LocalDateTime.parse("2018-10-10", df);------------------------2018-12-04T18:57:14

构造时间

 LocalDateTime startTime = LocalDateTime.of(2018, 1, 1, 20, 31, 20);

比较时间

 time.isAfter(startTime);//之后----------------------------true    
 time.isBefore(endTime);//之前---------------------------false

时间运算,相加相减

 time.plusYears(2); //加2年----------------------------2020-12-04T18:57:14.098
 time.minusYears(2); //减两年-------------------------2016-12-04T18:57:14.098
 time.plusDays(2); //加两天----------------------------2018-12-06T18:57:14.098
 time.minusDays(2); //减两天--------------------------2018-12-02T18:57:14.098

获取毫秒数(使用Instant)

 time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();------1543921034098

获取秒数(使用Instant)

 time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();-----1543921034

LocalDateTime 转 Date

LocalDateTime localDateTime=LocalDateTime.now()
Date date = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());

Date 转 LocalDateTime

Date startDate=new Date();
LocalDateTime localDateTime = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()
LocalDateTime now = LocalDateTime.now();
System.out.println("计算两个时间的差:");
LocalDateTime end = LocalDateTime.now();
Duration duration = Duration.between(now,end);
long days = duration.toDays(); //相差的天数
long hours = duration.toHours();//相差的小时数
long minutes = duration.toMinutes();//相差的分钟数
long millis = duration.toMillis();//相差毫秒数
long nanos = duration.toNanos();//相差的纳秒数
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值