• localDateTime和Date


    该配置类处理全局日期格式化。

    和 @JsonFormat一起使用时,@JsonFormat优先级更高。

     1 @Configuration
     2 public class LocalDateTimeSerializerConfig {
     3 
     4     @Value(value = "${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
     5     private String pattern;
     6 
     7     @Bean
     8     public LocalDateTimeSerializer localDateTimeDeserializer() {
     9         return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));
    10     }
    11 
    12     @Bean
    13     public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    14         return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
    15     }
    16 }

    ①:获取当前时间对象

    LocalDateTime now = LocalDateTime.now();
    Date now = new Date();

    ②:获取年月日

    LocalDate.now();

    ③:获取时分秒

    LocalTime.now()

    ④:LocalDateTime获取毫秒数

    //获取秒数
    Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
    //获取毫秒数
    Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
      
    LocalDateTime与String互转
    //时间转字符串格式化
     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
     String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
      
    //字符串转时间
    String dateTimeStr = "2018-07-28 14:11:15";
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, df);
     

    ⑤:Date与LocalDateTime互转

    //将java.util.Date 转换为java8 的java.time.LocalDateTime,默认时区为东8区
     public static LocalDateTime dateConvertToLocalDateTime(Date date) {
      return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
     }
      
      
     //将java8 的 java.time.LocalDateTime 转换为 java.util.Date,默认时区为东8区
     public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
      return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
     

    ⑥:LocalDateTime转为自定义的时间格式的字符串

    public static String getDateTimeAsString(LocalDateTime localDateTime, String format) {
     DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
     return localDateTime.format(formatter);
    }
     

    ⑦:某时间字符串转为自定义时间格式的LocalDateTime

    public static LocalDateTime parseStringToDateTime(String time, String format) {
     DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
     return LocalDateTime.parse(time, df);
    }
     

    ⑧:long类型的timestamp转为LocalDateTime

    public static LocalDateTime getDateTimeOfTimestamp(long timestamp) {
     Instant instant = Instant.ofEpochMilli(timestamp);
     ZoneId zone = ZoneId.systemDefault();
     return LocalDateTime.ofInstant(instant, zone);
    }
     

    ⑨:LocalDateTime转为long类型的timestamp

    public static long getTimestampOfDateTime(LocalDateTime localDateTime) {
     ZoneId zone = ZoneId.systemDefault();
     Instant instant = localDateTime.atZone(zone).toInstant();
     return instant.toEpochMilli();
    }


  • 相关阅读:
    月亮,还是馅饼(2)
    月亮,还是馅饼(1)
    spread 论坛
    kaok website
    提升 .NET 程序性能的 一些 原则
    sql convert
    sql 中 MSDTC 不可用。
    判断sql执行所花的时间(精度为毫秒)
    35岁以前成功的12条黄金法则
    快速删除表中的数据
  • 原文地址:https://www.cnblogs.com/rempop/p/12695233.html
Copyright © 2020-2023  润新知