零基础学Java第二十六天之日期类的使用

日期时间类、时间戳、间隔类
public class Test1 {
	@Test
	public void test01() {
		//LocalDate LocalTime LocalDateTime
		//这三个日期类的使用大致一样

		//获取当前日期时间对象
		LocalDateTime ldt1 = LocalDateTime.now();
		System.out.println(ldt1);//xxxx-xx-xxTxx:xx:xx

		//获取指定日期时间对象
		LocalDateTime ldt2 = LocalDateTime.of(2020, 1, 23, 8, 30, 10, 10);
		System.out.println(ldt2);//2020-1-23T8:30:10:10

		//获取ldt1推后的时间日期对象
		LocalDateTime ldt3 = ldt1.plusYears(2);
		System.out.println(ldt3);

		//获取ldt1提前的时间日期对象
		LocalDateTime ldt4 = ldt3.minusMonths(2);

		System.out.println(ldt4.getYear());
		System.out.println(ldt4.getMonthValue());
		System.out.println(ldt4.getDayOfMonth());
		System.out.println(ldt4.getHour());
		System.out.println(ldt4.getMinute());
		System.out.println(ldt4.getSecond());
	}
	@Test
	public void test02() {
		//使用时间戳(从1970年1月1日0:0:0到现在的毫秒值)

		//默认创建UTC(世界标准时间)时区的时间戳对象
		Instant now1 = Instant.now();
		System.out.println(now1);//2021-10-29T15:34:30

		//获取偏移8小时的偏移日期时间对象
		OffsetDateTime odt = now1.atOffset(ZoneOffset.ofHours(8));
		System.out.println(odt);

		//获取时间戳的毫秒值形式
		System.out.println(now1.toEpochMilli());

		//获取一个1970年1月1日0:0:0 往后退1秒的时间戳对象
		Instant now2 = Instant.ofEpochSecond(1);
		System.out.println(now2);
	}
	@Test
	public void test03() throws InterruptedException {
		//Duration:时间间隔类

		Instant now1 = Instant.now();
		Thread.sleep(1000);
		Instant now2 = Instant.now();
        
		//获取时间间隔类对象
		Duration duration1 = Duration.between(now1, now2);
		System.out.println(duration1.toMillis());
		
		System.out.println("-----------------------------");
		
		LocalTime lt1 = LocalTime.now();
		Thread.sleep(1000);
		LocalTime lt2 = LocalTime.now();
		//获取时间间隔类对象
		Duration duration2 = Duration.between(lt1, lt2);
		System.out.println(duration2.toMillis());
	}
	@Test
	public void test04() throws InterruptedException {
		//Period:日期间隔类
        
		LocalDate ld1 = LocalDate.now();
		Thread.sleep(1000);
		LocalDate ld2 = LocalDate.of(2021, 12, 31);
		
		Period period = Period.between(ld1, ld2);
		System.out.println(period.getYears());
		System.out.println(period.getMonths());
		System.out.println(period.getDays());
	}
}
日期时间格式化类-DateTimeFormatter
public class Test1 {
	@Test
	public void test01() {
		//格式化日期时间类
		
		LocalDateTime ldt1 = LocalDateTime.now();
		
		//获取本地标准的日期时间格式化对象
		DateTimeFormatter dtf1 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		String strDateTime1 = ldt1.format(dtf1);//格式化时间日期
		System.out.println(strDateTime1);
		
		//自定义日期时间格式化对象
		DateTimeFormatter dtf2 = DateTimeFormatter.
            ofPattern("yyyy年MM月dd日 HH:mm:ss");
		String strDateTime2 = ldt1.format(dtf2);//格式化时间日期
		System.out.println(strDateTime2);
		
		//将指定格式的字符串解析成LocalDateTime对象
		LocalDateTime parse = LocalDateTime.parse("2020年03月12日 11:04:14", dtf2);
		System.out.println(parse);
	}
}
时间矫正器类-TemporalAdjuster
public class Test1 {
	@Test
	public void test01() {
		//时间矫正器
		
		LocalDateTime ldt1 = LocalDateTime.now();
		
		//设置指定月份
		LocalDateTime ldt2 = ldt1.withMonth(10);
		System.out.println(ldt2);
		
		//设置下一个周末
		LocalDateTime ldt3 = ldt1.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
		System.out.println(ldt3);
		
		//自定义时间矫正器:设置下一个工作
		LocalDateTime ldt4 = ldt1.with((temporal)->{
			LocalDateTime ldt = (LocalDateTime) temporal;
			DayOfWeek week = ldt.getDayOfWeek();
			if(week.equals(DayOfWeek.FRIDAY)){//周五
				return ldt.plusDays(3);
			}else if(week.equals(DayOfWeek.SATURDAY)){//周六
				return ldt.plusDays(2);
			}else{
				return ldt.plusDays(1);
			}
		});
		System.out.println(ldt4);
	}
}
时区类
public class Test1 {
	@Test
	public void test01() {
		//时区类 
		
		//获取所有时区字符串
		Set<String> set = ZoneId.getAvailableZoneIds();
		for (String str : set) {
			System.out.println(str);
		}
		
		//获取指定时区的日期时间对象
		LocalDateTime ldt1 = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
		System.out.println(ldt1);
		
		//获取指定时区的日期时间对象 + 偏移量
		LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Asia/Tokyo"));
		ZonedDateTime zonedDateTime = ldt2.atZone(ZoneId.of("Asia/Tokyo"));
		System.out.println(zonedDateTime);
	}
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值