Java8新特性之新的日期和时间API

使用LocalDate、LocalTime、LocalDateTime

          LocalDate、LocalTime、LocalDateTime 类的实例是不可变的对象,分别表示使用ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的日期或时间,并不包含当前的时间信息。也不包含与时区相关的信息。java

注:ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法ui

//1.LocalDate、LocalTime、LocalDateTime
	@Test
	public void test1(){
		//获取当前时间
		LocalDateTime ldt = LocalDateTime.now();
		System.out.println(ldt);//2017-03-13T15:52:41.810
		//指定日期实例
		LocalDateTime ldt2 = LocalDateTime.of(2016, 5, 22, 14, 20, 22);
		System.out.println(ldt2);//2016-05-22T14:20:22
		//加一年
		LocalDateTime ldt3 = ldt.plusYears(1L);
		System.out.println(ldt3);//2018-03-13T15:53:51.812
		//分别去年月日时分秒
		System.out.println(ldt.getYear());//2017
		System.out.println(ldt.getMonthValue());//3
		System.out.println(ldt.getDayOfMonth());//13
		System.out.println(ldt.getHour());//15
		System.out.println(ldt.getMinute());//56
		System.out.println(ldt.getSecond());//22
	}

Instant 时间戳

   用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算spa

//2.Instant 时间戳
	@Test
	public void test2(){
		Instant in = Instant.now();//默认获取UTC时区时间(格林威治时间),与中国时间差8小时
		System.out.println(in);//2017-03-13T08:04:57.942Z
		OffsetDateTime offsetDateTime = in.atOffset(ZoneOffset.ofHours(8));
		System.out.println(offsetDateTime);//2017-03-13T16:04:57.942+08:00
		//获取时间戳
		System.out.println(in.toEpochMilli());//1489392373689
		//对UTC元年的计算
		Instant newIn = Instant.ofEpochSecond(60);
		System.out.println(newIn);//1970-01-01T00:01:00Z
	}

Duration 和Period

  • Duration:用于计算两个“时间”间隔
  • Period:用于计算两个“日期”间隔
    //3.Duration 和Period
    	/*Duration:用于计算两个“时间”间隔
    	Period:用于计算两个“日期”间隔*/
    	@Test
    	public void test3() throws InterruptedException {
    		//Duration:用于计算两个“时间”间隔
    		Instant in1 = Instant.now();
    		Thread.sleep(1000);
    		Instant in2 = Instant.now();
    		Duration d = Duration.between(in1, in2);
    		System.out.println(d.toMillis());//1000
    
    		System.out.println("-------------");
    
    		LocalDateTime ldt1 = LocalDateTime.now();
    		Thread.sleep(2000);
    		LocalDateTime ldt2 = LocalDateTime.now();
    		Duration d2 = Duration.between(ldt1, ldt2);
    		System.out.println(d2.toMillis());//2000
    
    		//Period:用于计算两个“日期”间隔
    		LocalDate ld = LocalDate.of(2015, 12, 20);
    		LocalDate ld2 = LocalDate.now();
    		Period p1 = Period.between(ld, ld2);
    		System.out.println(p1.getYears());//1
    		System.out.println(p1.getMonths());//2
    		System.out.println(p1.getDays());//21
    
    	}

 日期的操纵

  • TemporalAdjuster : 时间校订器。有时咱们可能须要获取例如:将日期调整到“下个周日”等操做。
  • TemporalAdjusters : 该类经过静态方法提供了大量的经常使用TemporalAdjuster 的实现
    //4.TemporalAdjuster : 时间校订器
    	@Test
    	public void test4(){
    		LocalDateTime ldt = LocalDateTime.now();
    		System.out.println(ldt);//2017-03-13T16:36:56.641
    
    		LocalDateTime ldt1 = ldt.withDayOfMonth(10);
    		System.out.println(ldt1);//2017-03-10T16:36:56.641
    
    		LocalDateTime ldt2 = ldt.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
    		System.out.println(ldt2);//2017-03-19T16:36:56.641
    
    		//自定义下一个周日
    		LocalDateTime ldt4 = ldt.with((l) ->{
    			LocalDateTime ldt3 = (LocalDateTime) l;
    			DayOfWeek dow = ldt3.getDayOfWeek();
    			if (dow.equals(DayOfWeek.SUNDAY)){
    				return ldt3.plusDays(7L);
    			}else if (dow.equals(DayOfWeek.MONDAY)){
    				return ldt3.plusDays(6L);
    			} else if (dow.equals(DayOfWeek.THURSDAY)){
    				return ldt3.plusDays(5L);
    			}else if (dow.equals(DayOfWeek.WEDNESDAY)){
    				return ldt3.plusDays(4L);
    			} else if (dow.equals(DayOfWeek.FRIDAY)){
    				return ldt3.plusDays(3L);
    			}else if(dow.equals(DayOfWeek.SATURDAY)){
    				return ldt3.plusDays(2L);
    			}else{
    				return ldt3.plusDays(1L);
    			}
    		});
    		System.out.println(ldt4);//2017-03-19T16:51:55.069
    	}

解析与格式化

java.time.format.DateTimeFormatter 类:该类提供了三种格式化方法:code

  •  预约义的标准格式
  • 语言环境相关的格式
  • 自定义的格式 
//5.DateTimeFormatter:解析与格式化
	@Test
	public void test5(){
		DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE;
		LocalDateTime ldt = LocalDateTime.now();

		String str = ldt.format(dtf);
		System.out.println(str);//2017-03-13

		System.out.println("-----------------------");
		//自定义本身的格式
		DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分ss秒");
		String str1 = dtf1.format(ldt);
		System.out.println(str1);//2017年03月13日17时13分26秒

		LocalDateTime ldt2 = LocalDateTime.parse(str1, dtf1);
		System.out.println(ldt2);//2017-03-13T17:13:26
	}

时区的处理

  • Java8 中加入了对时区的支持,带时区的时间为分别为:ZonedDate、ZonedTime、ZonedDateTime

其中每一个时区都对应着ID,地区ID都为“{区域}/{城市}”的格式例如:Asia/Shanghai 等
ZoneId:该类中包含了全部的时区信息
getAvailableZoneIds() : 能够获取全部时区时区信息
of(id) : 用指定的时区信息获取ZoneId 对象orm

//6.ZonedDate、ZonedTime、ZonedDateTime
	@Test
	public void test6(){
		//获取支持的全部时区
		Set<String> set =  ZoneId.getAvailableZoneIds();
		set.forEach(System.out::println);
	/*	Asia/Aden
		America/Cuiaba
		Etc/GMT+9
		Etc/GMT+8
		Africa/Nairobi
		America/Marigot
		Asia/Aqtau
		Pacific/Kwajalein
		America/El_Salvador
		Asia/Pontianak
		Africa/Cairo
		......
		*/
		LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
		System.out.println(ldt);//2017-03-13T17:22:15.483

		LocalDateTime ldt1 = LocalDateTime.now(ZoneId.of("Africa/Monrovia"));
		ZonedDateTime zdt = ldt1.atZone(ZoneId.of("Asia/Shanghai"));
		System.out.println(zdt);//2017-03-13T09:24:15.446+08:00[Asia/Shanghai]
	}
相关文章
相关标签/搜索