java8 localDateTime 、localDate 、date、字符串之间互操作

localDateTime 、localDate 、date、字符串之间互操作

  public static final String DATE_FMORAT_DATE_YIME = "yyyy-MM-dd HH:mm:ss";

    /**
     * 上海
     */
    public static final TimeZone TIME_ZONE = TimeZone.getTimeZone("Asia/Shanghai");

    /**
     * DateTimeFormatter yyyy-MM-dd HH:mm:ss
     */
    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * DateTimeFormatter yyyy-MM-dd
     */
    public static final DateTimeFormatter DATE_TIME_FORMATTER2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * DateTimeFormatter yyyy-MM-dd HH:mm
     */
    public static final DateTimeFormatter DATE_TIME_FORMATTER3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

Date 转 localDateTime 、localDate后再格式化为字符串

 Instant instant = t.getPsyxq().toInstant();
ZoneId zoneId = ZoneId.systemDefault();

LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

String psyxqStr=   DateSimpleUtil.DATE_TIME_FORMATTER.format(localDateTime.now());
           

localDateTime 转字符串

   String  excuteMintsDate = DateSimpleUtil.DATE_TIME_FORMATTER3.format(localDateTime);

字符串转 localDateTime

 LocalDateTime czsjLocalDateTime= LocalDateTime.parse(map.get("ZXRQ").toString(),DateSimpleUtil.DATE_TIME_FORMATTER);

localDateTime 转Date

LocalDateTime now = LocalDateTime.now();
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = now.atZone(zoneId).toInstant();
Date date =  Date.from(now.atZone(zoneId).toInstant());

Date 转localDateTime

    Instant instant =  bqyzxxDO.getLrsj().toInstant();
    ZoneId zoneId = ZoneId.systemDefault();
  LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
bqfyqqkDO.setYzLrrq(DateSimpleUtil.DATE_TIME_FORMATTER.format(localDateTime));```



你可能感兴趣的:(java基础)