JAVA 各种时间类型转换

final Date date = new Date();
    final Timestamp timestamp = new Timestamp(date.getTime());
    final Calendar calendar = Calendar.getInstance();
    
    final Instant instant = Instant.now();
    final LocalDateTime localDateTime = LocalDateTime.now();
    final ZonedDateTime zonedDateTime = ZonedDateTime.now();

initialize

转Date

//Timestamp转Date
    Date timestampDate = new Date(timestamp.getTime());
    //Calendar转Date
    Date calendarDate = calendar.getTime();
    //Instant转Date
    Date instantDate = Date.from(instant);
    //LocalDateTime转Date
    Date localDateTimeDate = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    //ZonedDateTime转Date
    Date zonedDateTimeDate = Date.from(zonedDateTime.toInstant());

转Instant

//Date转Instant
    Instant dateInstant = date.toInstant();
    //Timestamp转Instant
    Instant timestampInstant = timestamp.toInstant();
    //Calendar转Instant
    Instant calendarInstant = calendar.toInstant();
    //LocalDateTime转Instant
    Instant localDateTimeInstant = localDateTime.toInstant(ZoneOffset.of(ZoneId.systemDefault().getId()));
    //ZonedDateTime转Instant
    Instant zonedDateTimeInstant = zonedDateTime.toInstant();

转LocalDateTime

//Date转LocalDateTime
    LocalDateTime dateLocalDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    //Timestamp转LocalDateTime
    LocalDateTime timestampLocalDateTime = timestamp.toLocalDateTime();
    //Calendar转LocalDateTime
    LocalDateTime calendarLocalDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneOffset.systemDefault());
    //Instant转LocalDateTime
    LocalDateTime instantLocalDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    //ZonedDateTime转LocalDateTime
    LocalDateTime zonedDateTimeLocalDateTime = zonedDateTime.toLocalDateTime();

转ZonedDateTime

//Date转ZonedDateTime
    ZonedDateTime dateZonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    //Timestamp转ZonedDateTime
    ZonedDateTime timestampZonedDateTime = ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
    //Calendar转ZonedDateTime
    ZonedDateTime calendarZonedDateTime = ZonedDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
    //Instant转ZonedDateTime
    ZonedDateTime instantZonedDateTime = instant.atZone(ZoneId.systemDefault());
    //LocalDateTime转ZonedDateTime
    ZonedDateTime localDateTimeZonedDateTime = localDateTime.atZone(ZoneId.systemDefault());

转Timestamp

//Date转Timestamp
    Timestamp dateTimestamp = new Timestamp(date.getTime());
    //Calendar转Timestamp
    Timestamp calendarTimestamp = new Timestamp(calendar.getTimeInMillis());
    //Instant转Timestamp
    Timestamp instantTimestamp = Timestamp.from(instant);
    //LocalDateTime转Timestamp
    Timestamp localDateTimeTimestamp = Timestamp.valueOf(localDateTime);
    //ZonedDateTime转Timestamp
    Timestamp zonedDateTimeTimestamp = Timestamp.from(zonedDateTime.toInstant());

转Calendar

//Date转Calendar
    Calendar dateCalendar = Calendar.getInstance();
    dateCalendar.setTime(date);
    //Timestamp转Calendar
    Calendar timestampCalendar = Calendar.getInstance();
    timestampCalendar.setTimeInMillis(timestamp.getTime());
    //Instant转Calendar
    Calendar instantCalendar = GregorianCalendar.from(ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()));
    //LocalDateTime转Calendar
    Calendar localDateTimeCalendar = GregorianCalendar.from(ZonedDateTime.of(localDateTime, ZoneId.systemDefault()));
    //ZonedDateTime转Calendar
    Calendar zonedDateTimeInstantCalendar = GregorianCalendar.from(zonedDateTime);

 

posted @ 2018-11-29 17:15  `初一`  阅读(878)  评论(0编辑  收藏  举报