java8 LocalDate、LocalTime、LocalDateTime

LocalDate获取年月日
LocalDate today = LocalDate.now();
    System.out.println("今天的日期:" + today);
    System.out.println(
        "年月日是:" + today.getYear() + "年" + today.getMonthValue() + "月" + today.getDayOfMonth()
            + "日");
LocalTime获取时分秒
LocalDateTime获取年月日时分秒

根据时区获取时间

LocalDateTime.now(ZoneId.of("UTC"))
获取三天后的UTC时间

LocalDateTime.now(ZoneId.of("Asia/Shanghai")).plus(3,ChronoUnit.DAYS)
获取ZoneId  
 ZoneId.getAvailableZoneIds()

一、LocalDate相关

1.LocalDate获取某月第一天

LocalDate localDate = LocalDate.now();
localDate = localDate.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());

2.LocalDate格式化

public String formatLocalDate(LocalDate date, String format) {
    if (StringUtils.isBlank(format)) {
      return date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
    } else {
      return date.format(DateTimeFormatter.ofPattern(format));
    }
  }

3.LocalDate转换为Instant

LocalDate localDate = LocalDate.now();
Instant instant = localDate.atStartOfDay().toInstant(ZoneOffset.UTC);

4.LocalDate转换为LocalDateTime

LocalDate localDate = LocalDate.now();
LocalDateTime localDateTime = localDate.atStartOfDay();

二、LocalDateTime相关

1.LocalDateTime获取上月第一天

    LocalDateTime localDateTime = LocalDateTime.now();
    localDateTime = localDateTime.minusMonths(1).with(TemporalAdjusters.firstDayOfMonth());
    localDateTime = localDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
    //2021-12-01T00:00

2.LocalDateTime格式化

    LocalDateTime localDateTime = LocalDateTime.now();
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String format = localDateTime.format(fmt);
    //2022-01-10 10:04:22

3.LocalDateTime转换为Instant

    LocalDateTime localDateTime = LocalDateTime.now();
    ZoneId zoneId = ZoneId.of("Asia/Shanghai");
    ZonedDateTime zdt = localDateTime.atZone(zoneId);
    Instant instant = zdt.toInstant();
    //2022-01-10T02:16:27.834392400Z

4.LocalDateTime转换为LocalDate

    LocalDateTime localDateTime = LocalDateTime.now();
    LocalDate localDate = localDateTime.toLocalDate();
    //2022-01-10

三、Instant相关

1.Instant转换为LocalDateTime

    Instant instant = Instant.now();
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("UTC"));
    //2022-01-10T02:28:53.043126200

2.Instatnt转换为LocalDate

    Instant instant = Instant.now();
    LocalDate localDate = LocalDateTime.ofInstant(instant, ZoneId.of("UTC")).toLocalDate();
    //2022-01-10

3.Instant格式化

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss")
            .withLocale(Locale.CHINA)
            .withZone(ZoneId.systemDefault());
    String format = formatter.format(instant);
    //2022-01-10 10:40:38

4.Instant获取下一日的结束时间

Instant now = Instant.now();
Instant expiredDate = now.plus(Duration.ofDays(1));
LocalDateTime localDateTime = LocalDateTime.ofInstant(expiredDate, ZoneId.of("Asia/Shanghai"));
localDateTime = localDateTime.with(LocalTime.MAX);
//2022-01-11T23:59:59.999999999

5.两个日期相差多少毫秒

    Instant now = Instant.now();
    Instant expiredDate = now.plus(Duration.ofDays(1));
    long haomiao = ChronoUnit.MILLIS.between(Instant.now(), expiredDate);
    //86399998

四、Date String相关

1.日期字符串转换为LocalDateTime

    String dateString = "2022-01-10 10:40:38";
    DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    LocalDateTime ldt = LocalDateTime.parse(dateString, f);
    //2022-01-10T10:40:38

2.日期字符串转换为LocalDate

    String dateString = "2022-01-10 10:40:38";
    DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    LocalDateTime ldt = LocalDateTime.parse(dateString, f);
    LocalDate localDate = ldt.toLocalDate();
    //2022-01-10

3.日期字符串转换为Instant

    String dateString = "2022-01-10 10:40:38";
    DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    LocalDateTime ldt = LocalDateTime.parse(dateString, f);
    ZoneId zoneId = ZoneId.of("Asia/Shanghai");
    ZonedDateTime zonedDateTime = ldt.atZone(zoneId);
    Instant instant = zonedDateTime.toInstant();
    //2022-01-10T02:40:38Z

package com.aapoint.util; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAdjusters; public class LocalDateTimeUtil { /** * 比较 localDateTime2 是否在localDateTime1之前(比较大小) * @param localDateTime1 * @param localDateTime2 * @return */ public static Boolean compare(LocalDateTime localDateTime1,LocalDateTime localDateTime2){ return localDateTime1.isBefore(localDateTime2); } /** * 获取当前月份前/后的月份的第一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String firstDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的第一天 String firstDay = date.with(TemporalAdjusters.firstDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("第一天为:"+firstDay); return firstDay; } /** * 获取当前月份前/后的月份的最后一天 * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String lastDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,0,i); //获取该月份的最后一天 String lastDay = date.with(TemporalAdjusters.lastDayOfMonth()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("最后一天为:"+lastDay); return lastDay; } /** * 获取当时间前/后的时间(天) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainDay(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,1,i); //获取天 String day = date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // System.out.println("获取的时间为(天):"+day); return day; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainHours(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,2,i); //获取该月份的最后一天 String hours = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(小时):"+hours); return hours; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainMinutes(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,3,i); //获取该月份的最后一天 String minutes = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(分钟):"+minutes); return minutes; } /** * 获取当时间前/后的时间(小时) * @param i 指定距离当前月份的时间 * @param state 状态 0.当月 1.前 2.后 * @return */ public static String obtainSeconds(Integer state,Integer i){ LocalDateTime date = null; //type 类型 0.月 1.天 2.小时 3.分钟 4.秒 date = getLocalDateTime(state,4,i); //获取该月份的最后一天 String seconds = date.format(DateTimeFormatter.ofPattern("HH:mm:ss")); // System.out.println("获取的时间为(秒):"+seconds); return seconds; } public static void main(String[] args) { System.out.println("当前时间为:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); System.out.println("前一个月份的第一天为:"+LocalDateTimeUtil.firstDay(1,1)); System.out.println("前一个月份的最后一天为:"+LocalDateTimeUtil.lastDay(1,1)); System.out.println("当前时间的前一天为:"+LocalDateTimeUtil.obtainDay(1,1)); System.out.println("当前时间的后一天为:"+LocalDateTimeUtil.obtainDay(2,1)); System.out.println("当前时间的前一小时为:"+LocalDateTimeUtil.obtainHours(1,1)); System.out.println("当前时间的后一小时为:"+LocalDateTimeUtil.obtainHours(2,1)); System.out.println("当前时间的前一分钟为:"+LocalDateTimeUtil.obtainMinutes(1,1)); System.out.println("当前时间的后一分钟为:"+LocalDateTimeUtil.obtainMinutes(2,1)); System.out.println("当前时间的前一秒为:"+LocalDateTimeUtil.obtainSeconds(1,1)); System.out.println("当前时间的后一秒为:"+LocalDateTimeUtil.obtainSeconds(2,1)); } private static LocalDateTime getLocalDateTime(Integer state,Integer type,Integer i) { LocalDateTime date; if(state == 0){ date = LocalDateTime.now(); }else if(state == 1){ if(type == 0) { //获取月 date = LocalDateTime.now().minusMonths(i); }else if(type == 1){ //获取天 date = LocalDateTime.now().minusDays(i); }else if(type == 2){ //获取小时 date = LocalDateTime.now().minusHours(i); }else if(type == 3){ //获取分钟 date = LocalDateTime.now().minusMinutes(i);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

裂魂人1214

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值