Java8新特性-TocalDate,LocalTime,LocalDateTime API

/**
* TocalDate,LocalTime,LocalDateTime类的实例是不可变的对象。
*
* 分别使表示使用ISO-8601日历系统的日期,时间,日期和时间。他们提供了简单的日期或时间,
*/

@Test
	public void test1() {
		// 获取
		LocalTime lt = LocalTime.now(); // LocalTime
		LocalDate ld = LocalDate.now(); // localDate
		LocalDateTime ldt = LocalDateTime.now(); // localDateTime
		System.out.println(lt);
		System.out.println(ld.toEpochDay()); // 转到天数
		System.out.println(ldt);

		System.out.println(lt.getHour()); // 获取小时
		System.out.println(lt.getMinute()); // 获取秒

		// 指定
		LocalTime of = LocalTime.of(10, 15); // 10:15
		// ..
		System.out.println(of);

		// 相加
		LocalDate plusYears = ld.plusYears(1); // 当前日期加一年
		System.out.println(plusYears);
		// LocalTime : 支持加小时 ,分钟,毫秒等。LocalDate 支持加年份 天数等,,依次类推 方法都为plusXxx

		// 相减 同上, 方法都为minusXXX
		LocalTime minusHours = lt.minusHours(5);
		System.out.println(minusHours);

	}
// Instant 时间戳 / 时刻 (以Unix 元年 : 1970年1月1日 00:00:00 到某个时间之间的毫秒值
	@Test
	public void test2() {
		Instant ins1 = Instant.now(); // 默认获取UTC时区
		System.out.println(ins1);

		OffsetDateTime odt = ins1.atOffset(ZoneOffset.ofHours(8)); // 偏移8个时区
		System.out.println(odt);

		System.out.println(ins1.toEpochMilli()); // 当前时间戳

	}
// 计算
	@Test
	public void test3() {
		// Duration : 计算两个'时间'之间的间隔

		Instant inst1 = Instant.now();
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
		}
		Instant inst2 = Instant.now();
		Duration between = Duration.between(inst1, inst2); // locat*** 一样适用
		System.out.println(between.toMillis()); // 相隔毫秒

		System.out.println("===================");

		// Period : 计算两个'日期'之间的间隔

		LocalDate l = LocalDate.of(1999, 05, 29);
		LocalDate wy = LocalDate.of(1996, 03, 02);

		Period per = Period.between(l, wy);
		System.out.println(per.getYears()); // 年份相差多少

		// 如果想获取相隔多少天
		long day = wy.toEpochDay() - l.toEpochDay();
		System.out.println(day);
	}

	// 时间矫正器
	@Test
	public void test4() {

		// TemporalAdjuster : 时间矫正器
		// TemporalAdjusters : 该类通过静态方法提供了大量常用TemporalAdjuster的实现

		LocalDateTime now = LocalDateTime.now(); // 当前时间
		System.out.println(now);

		LocalDateTime withDayOfMonth = now.withDayOfMonth(10); // 把当前时间的月份改为10
		System.out.println(withDayOfMonth);

		LocalDateTime with = now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)); // 下一个周日是什么时候
		System.out.println(with);

		LocalDateTime w = LocalDateTime.of(2021, 1, 22, 0, 0);

		boolean after = w.isAfter(now);
		boolean before = w.isBefore(now);
		System.out.println(after);
		System.out.println(before);

		// 自定义: 下一个工作日是什么时候
		LocalDateTime with2 = w.with((n) -> {
			LocalDateTime ldt = (LocalDateTime) n;
			DayOfWeek dayOfWeek = ldt.getDayOfWeek();
			if (DayOfWeek.FRIDAY.equals(dayOfWeek)) {
				return ldt.plusDays(3);
			} else if (DayOfWeek.SATURDAY.equals(dayOfWeek)) {
				return ldt.plusDays(2);
			} else {
				return ldt.plusDays(1);
			}
		});

		System.out.println(with2);

	}

// DateTimeFormatter 格式化时间/日期
	@Test
	public void test5() {
		DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE;
		String format = isoDateTime.format(LocalDateTime.now());
		System.out.println(format);

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

		LocalDateTime parse = LocalDateTime.parse("2021-01-21 15:28:24", ofPattern);
		System.out.println(parse);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值