JAVA中的日期

获取当前的日期 LocalDate

  		LocalDate today = LocalDate.now();
		System.out.println("今天是:"+today);//今天是:2024-05-06
		String format = today.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
		System.out.println("今天是:" + format);//今天是:2024年05月06日
		String format1 = today.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
		System.out.println("今天是:" + format1);//今天是:2024年05月06日

获取年月日

  		LocalDate today = LocalDate.now();
		int year = today.getYear();
		int month = today.getMonthValue();
		int day = today.getDayOfMonth();
		//Year: 2024 Month: 5 Day: 6
		System.out.printf("Year: %d Month: %d Day: %d%n", year, month, day);

日期创建

  		LocalDate today = LocalDate.of(2017, 9, 1);
		//today = 2017-09-01
		System.out.println("today = " + today);

两个日期判断是否相等

  		LocalDate today = LocalDate.now();
		LocalDate before = LocalDate.of(2017, 9, 1);
		if (today.equals(before)) System.out.println("相等");
		else System.out.println("不相等");

YearMonth

  		YearMonth currentYearMonth = YearMonth.now();
		//当前是 2023-05 月 共: 31 天 %n 表示换行
		System.out.printf("当前是 %s  本月共: %d 天 %n", currentYearMonth, currentYearMonth.lengthOfMonth());
		YearMonth creditCardExpiry = YearMonth.of(2029, Month.APRIL);
		//你的信用卡将于 2029-04 到期
		System.out.printf("你的信用卡将于 %s 到期", creditCardExpiry);

周期性时间

  		LocalDate today = LocalDate.now();
		LocalDate birthdayDate = LocalDate.of(2017, 9, 1);
		MonthDay birthday = MonthDay.of(birthdayDate.getMonth(), birthdayDate.getDayOfMonth()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值