s-day01-DateTimeFormater类(代替SimpleDateFormat)
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
package com.inmind.jdk8_date05;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
/*
|
||||||
|
DateTimeFormater类(代替SimpleDateFormat)
|
||||||
|
目标:掌握JDK 8新增的DateTimeFormatter格式化器的用法。
|
||||||
|
*/
|
||||||
|
public class DateTimeFormaterDemo13 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//1.创建格式化对象
|
||||||
|
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||||
|
//2.对时间对象格式化
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
String nowStr = dtf.format(now);// 正向格式化
|
||||||
|
System.out.println(nowStr);
|
||||||
|
/* LocalDate localDate = LocalDate.now();
|
||||||
|
String ldStr = dtf.format(localDate);
|
||||||
|
System.out.println(ldStr);*/
|
||||||
|
|
||||||
|
//3.格式化时间,其实还有一种方案。
|
||||||
|
String nowStr1 = now.format(dtf);//反向格式化
|
||||||
|
System.out.println(nowStr1);
|
||||||
|
|
||||||
|
//4.解析时间:解析时间一般使用LocalDateTime提供的解析方法来解析。(String--->LocalDateTime)
|
||||||
|
System.out.println("-----------------");
|
||||||
|
String dateStr = "2029-12-12 12:12:11";
|
||||||
|
//能不能String转为时间对象,从而进行时间操作
|
||||||
|
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dtf);
|
||||||
|
System.out.println(localDateTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user