首页 > 代码库 > Java经典实例:使用DateFormatter来格式化日期时间

Java经典实例:使用DateFormatter来格式化日期时间

Java版本:1.8开始

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Created by Frank
 */
public class CurrentDatetime {
    public static void main(String[] args) {
        LocalDate dNow = LocalDate.now();
        System.out.println(dNow);
        LocalTime tNow = LocalTime.now();
        System.out.println(tNow);
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        System.out.println(df.format(LocalDateTime.now()));


        System.out.println(LocalDate.parse("2016/11/28", df));

        DateTimeFormatter nTZ = DateTimeFormatter.ofPattern("d MMMM, yyyy h:mm a");
        System.out.println(ZonedDateTime.now().format(nTZ));
    }
}

运行输出:

2016-11-28
17:11:34.131
2016-11-28T17:11:34.131
2016/11/28
2016-11-28
28 十一月, 2016 5:11 下午

 

Java经典实例:使用DateFormatter来格式化日期时间