Java教程

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

本文主要是介绍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);

    }

}

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

时区:

 

 

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新特性:时间格式化与时区的处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!