Java8日期时间类LocalDateTime格式化

Java8日期时间类LocalDateTime格式化_第1张图片

LocalDateTime日期时间格式化

LocalDateTime localDateTime = LocalDateTime.now()
System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

localDateTime.format(`DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

测试1

	public static void main(String...arguments)throws Exception{
		
		LocalDateTime now = LocalDateTime.now();		
		
		System.out.println(now);
		System.out.println(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		System.out.println(now.format(DateTimeFormatter.BASIC_ISO_DATE));
		System.out.println(now.format(DateTimeFormatter.ISO_DATE));
		System.out.println(now.format(DateTimeFormatter.ISO_DATE_TIME));
		System.out.println(now.format(DateTimeFormatter.ISO_TIME));
//		System.out.println(now.format(DateTimeFormatter.ISO_INSTANT));  //会报错
		System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE));
		System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
		System.out.println(now.format(DateTimeFormatter.ISO_LOCAL_TIME));
//		System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_DATE));   //会报错
//		System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));     //会报错
//		System.out.println(now.format(DateTimeFormatter.ISO_OFFSET_TIME));    //会报错
		System.out.println(now.format(DateTimeFormatter.ISO_ORDINAL_DATE));
		System.out.println(now.format(DateTimeFormatter.ISO_WEEK_DATE));
//		System.out.println(now.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));    //会报错
//		System.out.println(now.format(DateTimeFormatter.RFC_1123_DATE_TIME));   //会报错
	}

测试2

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TestLocalDateTime230612 {
	
	public static void main(String...arguments)throws Exception{
		
		LocalDateTime now = LocalDateTime.now();
		
		
		pln("原样toString()" , now.toString());
		pln("DateTimeFormatter.ofPattern(\"yyyy-MM-dd HH:mm:ss\")" , now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		pln("DateTimeFormatter.BASIC_ISO_DATE)" , now.format(DateTimeFormatter.BASIC_ISO_DATE));
		pln("DateTimeFormatter.ISO_DATE" , now.format(DateTimeFormatter.ISO_DATE));
		pln("DateTimeFormatter.ISO_DATE_TIME)" , now.format(DateTimeFormatter.ISO_DATE_TIME));
		pln("DateTimeFormatter.ISO_TIME)" , now.format(DateTimeFormatter.ISO_TIME));
//		pln("" , now.format(DateTimeFormatter.ISO_INSTANT));  //会报错
		pln("DateTimeFormatter.ISO_LOCAL_DATE)" , now.format(DateTimeFormatter.ISO_LOCAL_DATE));
		pln("DateTimeFormatter.ISO_LOCAL_DATE_TIME)" , now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
		pln("DateTimeFormatter.ISO_LOCAL_TIME)" , now.format(DateTimeFormatter.ISO_LOCAL_TIME));
//		pln("" , now.format(DateTimeFormatter.ISO_OFFSET_DATE));   //会报错
//		pln("" , now.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));     //会报错
//		pln("" , now.format(DateTimeFormatter.ISO_OFFSET_TIME));    //会报错
		pln("DateTimeFormatter.ISO_ORDINAL_DATE)" , now.format(DateTimeFormatter.ISO_ORDINAL_DATE));
		pln("DateTimeFormatter.ISO_WEEK_DATE)" , now.format(DateTimeFormatter.ISO_WEEK_DATE));
//		pln("" , now.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));    //会报错
//		pln("" , now.format(DateTimeFormatter.RFC_1123_DATE_TIME));   //会报错
	}
	
	static void pln(String a, Object b) {
		while(a.length()<"DateTimeFormatter.ofPattern(\"yyyy-MM-dd HH:mm:ss\")".length())a+=" ";
		System.out.println  (a + "  的效果:  " + b);
	}	
}

测试2的结果

原样toString()                                       的效果:  2023-06-12T22:50:54.825257400
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")  的效果:  2023-06-12 22:50:54
DateTimeFormatter.BASIC_ISO_DATE)                   的效果:  20230612
DateTimeFormatter.ISO_DATE                          的效果:  2023-06-12
DateTimeFormatter.ISO_DATE_TIME)                    的效果:  2023-06-12T22:50:54.8252574
DateTimeFormatter.ISO_TIME)                         的效果:  22:50:54.8252574
DateTimeFormatter.ISO_LOCAL_DATE)                   的效果:  2023-06-12
DateTimeFormatter.ISO_LOCAL_DATE_TIME)              的效果:  2023-06-12T22:50:54.8252574
DateTimeFormatter.ISO_LOCAL_TIME)                   的效果:  22:50:54.8252574
DateTimeFormatter.ISO_ORDINAL_DATE)                 的效果:  2023-163
DateTimeFormatter.ISO_WEEK_DATE)                    的效果:  2023-W24-1

你可能感兴趣的:(JAVA,日期时间时区等,java)