关于LocalDate和LocalDateTime的一些操作

it2023-02-25  78

/**  * 设置日期格式化  * @type {[type]}  */ private static final DateTimeFormatter DATE_TIME_FORMATTER=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSS"); /**  * 设置日期时区常量  */  private static final ZoneId CHINA_ZONE_ID=ZoneId.systemDefault();

/**  * Date格式化为LocalDateTime  */ public void dateToDateTime(){     Date date=new Date();     LocalDateTime dateTime=date.Instant().atZone(CHINA_ZONE_ID).toLocalDateTime();     System.out.println(dateTime); }

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

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

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

/**日期加减**/ 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)); }

/**日期时间间隔**/ 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最后一天**/ 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); }

最新回复(0)