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 </span>= DateTimeFormatter.ofPattern("yyyy/MM/dd"<span style="color: #000000;">);
     System.out.println(df.format(LocalDateTime.now()));
    
    
     System.out.println(LocalDate.parse(</span>"2016/11/28"<span style="color: #000000;">, df));
    
     DateTimeFormatter nTZ </span>= DateTimeFormatter.ofPattern("d MMMM, yyyy h:mm a"<span style="color: #000000;">);
     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 下午
复制代码

原文地址:https://www.cnblogs.com/frankyou/p/6110393.html

posted @ 2019-07-19 11:43  星朝  阅读(1847)  评论(0编辑  收藏  举报