JAVA8 LocalDateTime时间 使用案例

简介:

新时间日期API常用、重要对象介绍:

  • ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则
  • Instant: 用来表示时间线上的一个点(瞬时)
  • LocalDate: 表示没有时区的日期, LocalDate是不可变并且线程安全的
  • LocalTime: 表示没有时区的时间, LocalTime是不可变并且线程安全的
  • LocalDateTime: 表示没有时区的日期时间, LocalDateTime是不可变并且线程安全的
  • Clock: 用于访问当前时刻、日期、时间,用到时区
  • Duration: 用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔
  • Period: 用于计算两个“日期”间隔

其中,LocalDate、LocalTime、LocalDateTime是新API里的基础对象,绝大多数操作都是围绕这几个对象来进行的,有必要搞清楚:

  1. LocalDate : 只含年月日的日期对象
  2. LocalTime :只含时分秒的时间对象
  3. LocalDateTime : 同时含有年月日时分秒的日期对象

本文将以实例讲解日常开发中常用到的时间日期操作,如:

  • 获取当前日期、时间
  • 指定时间日期创建对应的对象
  • 计算两个时间点的间隔
  • 判断两个时间的前后
  • 时间日期的格式化
  • 获取时间戳
  • 时间、日期相加减
  • 获取给定时间点的年份、月份、周、星期等

使用案列:

//获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println("当前时间为:"+now);

在这里插入图片描述

//根据指定日期/时间创建对象
LocalDateTime of = LocalDateTime.of(1997, 10, 10, 00, 01, 46);
System.out.println("根据指定日期/时间创建对象为:"+of);

在这里插入图片描述

//日期时间的加减
//plus(long amountToAdd, TemporalUnit unit) 参数1 : 相加的数量, 参数2 : 相加的单位
LocalDateTime plus = LocalDateTime.now().plus(2, ChronoUnit.DAYS); //在今天的日期加两天,得到后天的日期
System.out.println("后天的日期为:"+plus);

在这里插入图片描述

//将年、月、日等修改为指定的值,并返回新的日期(时间)对象
LocalDate localDate = LocalDate.now();
//当前时间基础上,指定本年当中的第几天,取值范围为1-365,366
LocalDate withDayOfYearResult = localDate.withDayOfYear(200);
//当前时间基础上,指定本月当中的第几天,取值范围为1-29,30,31
LocalDate withDayOfMonthResult = localDate.withDayOfMonth(5);
//当前时间基础上,直接指定年份
LocalDate withYearResult = localDate.withYear(2017);
//当前时间基础上,直接指定月份
LocalDate withMonthResult = localDate.withMonth(5);
System.out.println("当前时间是 : " + localDate + "\n"
        + "指定本年当中的第200天 : " + withDayOfYearResult + "\n"
        + "指定本月当中的第5天 : " + withDayOfMonthResult + "\n"
        + "直接指定年份为2017 : " + withYearResult + "\n"
        + "直接指定月份为5月 : " + withMonthResult + "\n"
);

在这里插入图片描述

//获取当天时间的年月日时分秒
int year = LocalDateTime.now().getYear();
Month month = LocalDateTime.now().getMonth();
int day = LocalDateTime.now().getDayOfMonth();
int hour = LocalDateTime.now().getHour();
int minute = LocalDateTime.now().getMinute();
int second = LocalDateTime.now().getSecond();
System.out.println("今天是" + LocalDateTime.now() + "\n"
        + "年 : " + year + "\n"
        + "月 : " + month.getValue() + "-即 "+ month + "\n"
        + "日 : " + day + "\n"
        + "时 : " + hour + "\n"
        + "分 : " + minute + "\n"
        + "秒 : " + second + "\n"
);

在这里插入图片描述

//获取当前时间是本年或本月或本周的时间
LocalDateTime localDateTime = LocalDateTime.now();
int dayOfYear = localDateTime.getDayOfYear();
int dayOfMonth = localDateTime.getDayOfMonth();
DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
System.out.println("今天是" + localDateTime + "\n"
        + "本年当中第" + dayOfYear + "天" + "\n"
        + "本月当中第" + dayOfMonth + "天" + "\n"
        + "本周中星期" + dayOfWeek.getValue() + "-即" + dayOfWeek + "\n"
);

在这里插入图片描述

//时间日期前后的比较与判断
//判断两个时间点的前后
LocalDate localDate1 = LocalDate.of(2017, 8, 8);
LocalDate localDate2 = LocalDate.of(2018, 8, 8);
boolean date1IsBeforeDate2 = localDate1.isBefore(localDate2);
System.out.println("date1IsBeforeDate2 : " + date1IsBeforeDate2);
        // date1IsBeforeDate2 == true
//获取当天的早上6点
LocalDateTime six = LocalDateTime.of(LocalDate.now(), LocalTime.parse("06:00:00"));
System.out.println("当天的早上6点为:"+six);

在这里插入图片描述

//得到昨天的时间
LocalDateTime dateTime = LocalDateTime.now().minusDays(1);
System.out.println("昨天的时间为:"+dateTime);

在这里插入图片描述

//转成时间戳
long l = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
System.out.println("转成13位时间戳:"+l);
System.out.println("转成10位时间戳:"+l/1000);

在这里插入图片描述

//LocalDateTime转成String类型的时间,String类型的时间转成LocalDateTime
DateTimeFormatter df =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
String localTime = df.format(time);
LocalDateTime ldt = LocalDateTime.parse("2018-01-12 17:07:05",df);
System.out.println("LocalDateTime转成String类型的时间:"+localTime);
System.out.println("String类型的时间转成LocalDateTime:"+ldt);

在这里插入图片描述

//获得当前时间上个月的第一天和最后一天
LocalDate lastMonth = LocalDate.now().minusMonths(1);
LocalDate firstDayLasrMonth = lastMonth.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("上个月的第一天为:"+firstDayLasrMonth);
LocalDate lastDayLasrMonth = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("上个月的最后一天为:"+lastDayLasrMonth);

在这里插入图片描述

 //格式:2019-11-05 - 2019-11-05
String sTime = toShopTime.substring(0, 10) + " 00:00:00";
DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
toShopTimeStart = Math.toIntExact(LocalDateTime.from(LocalDateTime.parse(sTime, ftf)).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000);
String eTime = toShopTime.substring(13) + " 23:59:59";
toShopTimeEnd = Math.toIntExact(LocalDateTime.from(LocalDateTime.parse(eTime, ftf)).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000);

在这里插入图片描述

//计算两个时间的差
LocalDateTime now = LocalDateTime.now();
System.out.println("计算两个时间的差:");
Instant instant = Instant.ofEpochSecond(1581567312);//10位
//Instant instant = Instant.ofEpochMilli(1581561198111L); //13位时间戳
LocalDateTime end = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
Duration duration = Duration.between(end,now);
long days = duration.toDays(); //相差的天数
long hours = duration.toHours();//相差的小时数
long minutes = duration.toMinutes();//相差的分钟数
long millis = duration.toMillis();//相差毫秒数
long nanos = duration.toNanos();//相差的纳秒数
System.out.println(now);
System.out.println(end);
System.out.println("发送短信耗时【 "+days+"天:"+hours+" 小时:"+minutes+" 分钟:"+millis+" 毫秒:"+nanos+" 纳秒】");

在这里插入图片描述

//根据一个时间,得到这个时间所在的周的星期一和星期日的日期。
LocalDate from=LocalDate.now();
java.time.DayOfWeek date=from.getDayOfWeek();
LocalDate day1=from.with(TemporalAdjusters.previous(java.time.DayOfWeek.SUNDAY));
LocalDate day2=from.with(TemporalAdjusters.previous(java.time.DayOfWeek.SUNDAY)).plusDays(1);
LocalDate day3=from.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY)).minusDays(1);
System.out.println(from);//随便拟定一个日期
System.out.println(date);//求出这个日期是星期几
System.out.println(day1);//求出这个日期所在周的上一星期的星期日(日历一般第一天是周日开始)
System.out.println(day2);//求出这个日期所在周的星期一
System.out.println(day3);//求出这个日期所在周的星期日

在这里插入图片描述

参考文章:
https://www.jianshu.com/p/048ee8580639

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值