LocalDate

This commit is contained in:
seahi 2024-11-13 21:27:09 +08:00
parent ff273fbcde
commit 78135ec799
2 changed files with 18 additions and 1 deletions

View File

@ -1,4 +1,12 @@
package chapter5;
import java.time.Instant;
public class Example20 {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now);
Instant instant = Instant.ofEpochMilli(1000 * 60 * 60 * 24);
System.out.println(instant);
}
}

View File

@ -14,11 +14,20 @@ public class Example21 {
System.out.println("今年中的第几天:" + now.getDayOfYear());
System.out.println("周几:" + now.getDayOfWeek());
System.out.println("====判断相关的方法====");
System.out.println("\n====判断相关的方法====");
System.out.println("of是否在now之前" + of.isBefore(now));
System.out.println("of是否在now之后" + of.isAfter(now));
System.out.println("of和now是否相等" + of.isEqual(now));
System.out.println("of是否闰年" + of.isLeapYear());
System.out.println("\n====解析和加减操作====");
String dateStr = "2020-02-12";
LocalDate date = LocalDate.parse(dateStr);
System.out.println("把字符串转换为日期对象:" + date);
System.out.println("年份加1:" + date.plusYears(1));
System.out.println("天数减5:" + date.minusDays(5));
System.out.println("指定年份为2024" + date.withYear(2024));
}
}