Java8新特性-019-与传统日期处理的转换

To遗留类 From遗留类
java.time.Instant java.util.Date Date.from(instant) date.toInstant()
java.time.Instant java.sql.Timestamp Timestamp.from(instant) timestamp.toInstant()
java.time.ZonedDateTime java.util.GregorianCalendar GregorianCalendar.from(zonedDateTime) cal.toZonedDateTime()
java.time.LocalDate java.sql.Date Date.valueOf(localDate) date.toLocalDate()
java.time.LocalTime java.sql.Time Date.valueOf(localDate) date.toLocalTime()
java.time.LocalDateTime java.sql.Timestamp Timestamp.valueOf(localDateTime) timestamp.toLocalDateTime()
java.time.ZoneId java.util.TimeZone Timezone.getTimeZone(id) timeZone.toZoneId() java.time.format.DateTimeFormatter
java.text.DateFormat formatter.toFormat()

测试代码

TransferTimeTest.java

package java8.time;

import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.GregorianCalendar;

import org.junit.Test;

public class TransferTimeTest {
    
    /*
     * 1.java.time.Instant <----> java.util.Date  
     *      TO遗留类                   FROM遗留类
     *      Date.from(instant)      date.toInstant()
     */
    @Test
    public void test1() {
        Date date = new Date();
        Instant ins = Instant.now();
        System.out.println(date);
        System.out.println(ins);
        
        // TO遗留类(Date.from方法)
        Date from = Date.from(ins);
        System.out.println("我是从Instant转换成Date的!" + from);
        
        // FROM遗留类(date.toInstant)
        Instant instant = date.toInstant();
        System.out.println("我是从Date转换成Instant的!" + instant);
    }
    
    /*
     * 2.java.time.Instant <----> java.sql.Timestamp 
     *      TO遗留类                       FROM遗留类
     *      Timestamp.from(instant)     timestamp.toInstant()
     */
    @Test
    public void test2() {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        Instant ins1 = Instant.now();
        System.out.println(timestamp);
        System.out.println(ins1);
        
        // TO遗留类(Timestamp.from(instant)
        Timestamp from = Timestamp.from(ins1);
        System.out.println("我是从Instant转换成Timestamp的!" + from);
        
        // FROM遗留类(timestamp.toInstant)
        Instant instant = timestamp.toInstant();
        System.out.println("我是从Timestamp转换成Instant的!" + instant);
    }
    
    /*
     * 3.java.time.ZonedDateTime <----> java.util.GregorianCalendar 
     *      TO遗留类                                       FROM遗留类
     *      GregorianCalendar.from(zonedDateTime)       cal.toZonedDateTime()
     */
    @Test
    public void test3() {
        ZonedDateTime zdt = ZonedDateTime.now();
        GregorianCalendar cal = new GregorianCalendar();
        System.out.println(zdt);
        System.out.println(cal);
        
        // TO遗留类(GregorianCalendar.from方法)
        GregorianCalendar from = GregorianCalendar.from(zdt);
        System.out.println("我是从ZonedDateTime转换成GregorianCalendar的!" + from);
        
        // FROM遗留类(cal.toZonedDateTime方法)
        ZonedDateTime zonedDateTime = cal.toZonedDateTime();
        System.out.println("我是从GregorianCalendar转换成ZonedDateTime的!" + zonedDateTime);
    }
    
    /*
     * 4.java.time.LocalDate <----> java.sql.Date
     *      TO遗留类                       FROM遗留类
     *      Date.valueOf(localDate)     date.toLocalDate()
     */
    @Test
    public void test4() {
        LocalDate ld = LocalDate.now();
        java.sql.Date date = new java.sql.Date(new Date().getTime());
        System.out.println(ld);
        System.out.println(date);
        
        // TO遗留类(Date.valueOf方法)
        java.sql.Date valueOf = java.sql.Date.valueOf(ld);
        System.out.println("我是从LocalDate转换成Date的!" + valueOf);
    
        // FROM遗留类(date.toLocalDate方法)
        LocalDate localDate = date.toLocalDate();
        System.out.println("我是从Date转换成LocalDate的!" + localDate);
    }
    
    /*
     * ……
     */
}

微信公众号:JavaWeb架构师

其它

  • 源码下载
关注下方公众号,回复:Java8.code
  • 欢迎加入交流群:451826376

  • 更多信息:www.itcourse.top

完整教程PDF版本下载

你可能感兴趣的:(Java8新特性-019-与传统日期处理的转换)