JDK8日期、时间的使用
1 import org.junit.Test; 2 3 import java.time.LocalDate; 4 import java.time.LocalDateTime; 5 import java.time.LocalTime; 6 7 /**
8 * LocalDate,LocalTime,LocalDateTime的使用 9 * @author fu jingchao 10 * @creat 2021/12/2-17:22 11 */ 12 public class JDK8DateTimeTest { 13 @Test 14 public void test1(){ 15 //now():获取当前的日期、时间、日期+时间 16 LocalDate localDate = LocalDate.now(); 17 LocalTime localTime = LocalTime.now(); 18 LocalDateTime localDateTime = LocalDateTime.now(); 19 System.out.println(localDate);//2021-12-02 20 System.out.println(localTime);//17:43:43.962 21 System.out.println(localDateTime);//2021-12-02T17:43:43.962 22 23 //getXxx():获取相关的属性 24 System.out.println(localDateTime.getDayOfMonth()); 25 System.out.println(localDateTime.getDayOfWeek()); 26 System.out.println(localDateTime.getMonth()); 27 28 //of:指定时间创建对象 29 LocalDateTime localDateTime1 = LocalDateTime.of(2021,12,2,17, 30 50,28); 31 System.out.println(localDateTime1);//2021-12-02T17:50:28 32 33 //体现不可变性 34 //withXxx():设置相关的属性 35 LocalDate localDate1 = localDate.withDayOfMonth(22); 36 System.out.println(localDate);//2021-12-02 37 System.out.println(localDate1);//2021-12-22 38 39 LocalDateTime localDateTime2 = localDateTime.withHour(4);//相当于设置成4点 40 System.out.println(localDateTime);//2021-12-03T14:09:37.680 41 System.out.println(localDateTime2);//2021-12-03T04:09:37.680 42 43 //加 44 LocalDateTime localDateTime3 = localDateTime.plusMonths(3); 45 System.out.println(localDateTime);//2021-12-03T14:09:37.680 46 System.out.println(localDateTime3);//2022-03-03T14:09:37.680 47 48 //减 49 LocalDateTime localDateTime4 = localDateTime.minusDays(3); 50 System.out.println(localDateTime);//2021-12-03T14:13:03.345 51 System.out.println(localDateTime4);//2021-11-30T14:13:03.345 52 } 53 }
1 /* 2 Instant的使用 3 类似于java.util.Date类 4 5 */ 6 @Test 7 public void test2(){ 8 //now()获取本初子午线对应的标准时间 9 Instant instant = Instant.now(); 10 System.out.println(instant);//2021-12-03T07:36:54.266Z 11 12 //东八区(北京时间) 13 OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8)); 14 System.out.println(offsetDateTime);//2021-12-03T15:36:54.266+08:00 15 16 //获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数---->Date类的getTime() 17 long mill = instant.toEpochMilli(); 18 System.out.println(mill);//1638517411713 19 20 //ofEpochMilli():通过指定的毫秒数,获取Instant实例------>Date(long millis) 21 Instant instant1 = Instant.ofEpochMilli(1638517411713L); 22 System.out.println(instant1);//2021-12-03T07:43:31.713Z 23 24 }
1 /* 2 DateTimeFormatter:格式化或解析日期、时间 3 类似于SimpleDateFormat 4 */ 5 @Test 6 public void test3(){ 7 //方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME 8 DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; 9 LocalDateTime localDateTime = LocalDateTime.now(); 10 //格式化:日期---->字符串 11 String str1 = formatter.format(localDateTime); 12 System.out.println(localDateTime);//2021-12-03T16:14:16.243 13 System.out.println(str1);//2021-12-03T16:14:16.243 14 System.out.println(localDateTime.getClass());//class java.time.LocalDateTime 15 System.out.println(str1.getClass());//class java.lang.String 16 17 //解析:字符串---->日期 18 TemporalAccessor parse = formatter.parse("2021-12-03T16:14:16.243"); 19 System.out.println(parse);//{},ISO resolved to 2021-12-03T16:14:16.243 20 21 //方式二:本地化相关的格式。如:ofLocalizedDateTime(FormatStyle.LONG) 22 //FormatStyle.LONG/ FormatStyle.MEDIUM/ FormatStyle.SHORT:适用于LocalDateTime 23 DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT); 24 DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG); 25 //格式化 26 String str2 = formatter1.format(localDateTime); 27 System.out.println(str2);//21-12-3 下午4:30 28 29 String str3 = formatter2.format(localDateTime); 30 System.out.println(str3);//2021年12月3日 下午04时30分39秒 31 32 //本地化相关的格式。如:ofLocalizedDate() 33 //FormatStyle.FULL/ FormatStyle.LONG/ FormatStyle.MEDIUM/ FormatStyle.SHORT:适用于LocalDate 34 DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL); 35 //格式化 36 String str4 = formatter3.format(LocalDate.now()); 37 System.out.println(str4);//2021年12月3日 星期五 38 39 DateTimeFormatter formatter4 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); 40 //格式化 41 String str5 = formatter4.format(LocalDate.now()); 42 System.out.println(str5);//2021-12-3 43 44 //方式三:自定义的格式:如:ofPattern("yyyy-MM-dd hh:mm:ss E") 45 DateTimeFormatter formatter5 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"); 46 //格式化 47 String str6 = formatter5.format(LocalDateTime.now()); 48 System.out.println(str6);//2021-12-03 04:44:10 49 50 //解析 51 TemporalAccessor parse1 = formatter5.parse("2021-12-03 04:44:10"); 52 System.out.println(parse1);//{NanoOfSecond=0, MilliOfSecond=0, MinuteOfHour=44, HourOfAmPm=4, SecondOfMinute=10, MicroOfSecond=0},ISO resolved to 2021-12-03 53 54 55 }
此为本人学习笔记,若有错误,请不吝赐教