1. 程式人生 > >JAVA 各種時間型別轉換

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);