zoukankan      html  css  js  c++  java
  • JSR310 时间类型的相互转换

    参数申明:

    1   final Date date = new Date();
    2   final Timestamp timestamp = new Timestamp(date.getTime());
    3   final Calendar calendar = Calendar.getInstance();
    4   final Instant instant = Instant.now();
    5   final LocalDateTime localDateTime = LocalDateTime.now();
    6   final ZonedDateTime zonedDateTime = ZonedDateTime.now();
    7   final LocalDate localDate = LocalDate.now();

    转Instant

     1     //Date转Instant
     2     Instant dateInstant = date.toInstant();
     3     //Timestamp转Instant
     4     Instant timestampInstant = timestamp.toInstant();
     5     //Calendar转Instant
     6     Instant calendarInstant = calendar.toInstant();
     7     //LocalDateTime转Instant
     8     Instant localDateTimeInstant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
     9     //ZonedDateTime转Instant
    10     Instant zonedDateTimeInstant = zonedDateTime.toInstant();
    11     //LocalDate转Instant
    12     Instant localDateInstant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();

    转LocalDateTime

     1     //Date转LocalDateTime
     2     LocalDateTime dateLocalDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
     3     //Timestamp转LocalDateTime
     4     LocalDateTime timestampLocalDateTime = timestamp.toLocalDateTime();
     5     //Calendar转LocalDateTime
     6     LocalDateTime calendarLocalDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());
     7     //Instant转LocalDateTime
     8     LocalDateTime instantLocalDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
     9     //ZonedDateTime转LocalDateTime
    10     LocalDateTime zonedDateTimeLocalDateTime = zonedDateTime.toLocalDateTime();
    11     //LocalDate转LocalDateTime
    12     LocalDateTime localDateLocalDateTime = localDate.atStartOfDay();

    转ZonedDateTime

     1     //Date转ZonedDateTime
     2     ZonedDateTime dateZonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
     3     //Timestamp转ZonedDateTime
     4     ZonedDateTime timestampZonedDateTime = ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
     5     //Calendar转ZonedDateTime
     6     ZonedDateTime calendarZonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
     7     //Instant转ZonedDateTime
     8     ZonedDateTime instantZonedDateTime = instant.atZone(ZoneId.systemDefault());
     9     //LocalDateTime转ZonedDateTime
    10     ZonedDateTime localDateTimeZonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
    11     //LocalDate转ZonedDateTime
    12     ZonedDateTime localDateZonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());

    转Date

     1     //Timestamp转Date
     2     Date timestampDate = new Date(timestamp.getTime());
     3     //Calendar转Date
     4     Date calendarDate = calendar.getTime();
     5     //Instant转Date
     6     Date instantDate = Date.from(instant);
     7     //LocalDateTime转Date
     8     Date localDateTimeDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
     9     //ZonedDateTime转Date
    10     Date zonedDateTimeDate = Date.from(zonedDateTime.toInstant());
    11     //LocalDate转Date
    12     Date localDateDate = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

    转Timestamp

     1     //Date转Timestamp
     2     Timestamp dateTimestamp = new Timestamp(date.getTime());
     3     //Calendar转Timestamp
     4     Timestamp calendarTimestamp = new Timestamp(calendar.getTimeInMillis());
     5     //Instant转Timestamp
     6     Timestamp instantTimestamp = Timestamp.from(instant);
     7     //LocalDateTime转Timestamp
     8     Timestamp localDateTimeTimestamp = Timestamp.valueOf(localDateTime);
     9     //ZonedDateTime转Timestamp
    10     Timestamp zonedDateTimeTimestamp = Timestamp.from(zonedDateTime.toInstant());
    11     //LocalDate转Timestamp
    12     Timestamp localDateTimestamp = Timestamp.valueOf(localDate.atStartOfDay());

    转Calendar

     1     //Date转Calendar
     2     Calendar dateCalendar = Calendar.getInstance();
     3     dateCalendar.setTime(date);
     4     //Timestamp转Calendar
     5     Calendar timestampCalendar = Calendar.getInstance();
     6     timestampCalendar.setTimeInMillis(timestamp.getTime());
     7     //Instant转Calendar
     8     Calendar instantCalendar = GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));
     9     //LocalDateTime转Calendar
    10     Calendar localDateTimeCalendar = GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
    11     //ZonedDateTime转Calendar
    12     Calendar zonedDateTimeInstantCalendar = GregorianCalendar.from(zonedDateTime);
    13     //LocalDate转Calendar
    14     Calendar localDateCalendar = GregorianCalendar.from(localDate.atStartOfDay(ZoneId.systemDefault()));

    转LocalDate

     1     //Date转LocalDate
     2     LocalDate dateLocalDate = LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());  //jdk11
     3     LocalDate dateLocalDate = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
     4     //Timestamp转LocalDate
     5     LocalDate timestampLocalDate = timestamp.toLocalDateTime().toLocalDate();
     6     //Calendar转LocalDate
     7     LocalDate calendarLocalDate = LocalDate.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());  //jdk11
     8     LocalDate calendarLocalDate = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
     9     //Instant转LocalDate
    10     LocalDate instantLocalDate = LocalDate.ofInstant(instant, ZoneId.systemDefault());  //jdk11
    11     LocalDate instantLocalDate = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();
    12     //LocalDateTime转LocalDate
    13     LocalDate localDateTimeLocalDate = localDateTime.toLocalDate();
    14     //ZonedDateTime转LocalDate
    15     LocalDate zonedDateTimeLocalDate = zonedDateTime.toLocalDate();
  • 相关阅读:
    构建之法:第二次心得
    构建之法:第一次心得
    tomcat配置限制ip和建立图片服务器
    tomcat8.5优化配置
    java 操作 csv文件
    jsoup教学系列
    (转)js实现倒计时效果(年月日时分秒)
    本地启动tomcat的时候报java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: PermGen space
    使用mybatis执行oracle存储过程
    java 获取web登录者的ip地址
  • 原文地址:https://www.cnblogs.com/niceboat/p/7027394.html
Copyright © 2011-2022 走看看