Java8新特性:时间格式化与时区的处理

时间格式化:

package com;


import java.time.*;
import java.time.format.DateTimeFormatter;

public class Main1 {
    public static void main(String[] args){
        LocalDateTime now = LocalDateTime.now();
        String isoDateTime = now.format(DateTimeFormatter.ISO_DATE_TIME);
        System.out.println("isoDateTime = " + isoDateTime);

        String isoDate = now.format(DateTimeFormatter.ISO_DATE);
        System.out.println("isoDate = " + isoDate);

        String my = now.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss"));
        System.out.println("my = " + my);

    }

}

Java8新特性:时间格式化与时区的处理

======================

时区:

Java8新特性:时间格式化与时区的处理

 

 

package com;


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main1 {
    public static void main(String[] args){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime now1 = LocalDateTime.now(ZoneId.of("Europe/Moscow"));
        ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println("now = " + now);
        System.out.println("now1 = " + now1);
        System.out.println("zonedDateTime = " + zonedDateTime);

    }

}

Java8新特性:时间格式化与时区的处理

 

上一篇:Java 日期时间使用总结


下一篇:Java日期和时间类总结