Compare commits
3 Commits
ff273fbcde
...
9c663672aa
Author | SHA1 | Date | |
---|---|---|---|
9c663672aa | |||
4d4a3beb6a | |||
78135ec799 |
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
30
src/chapter5/Example22.java
Normal file
30
src/chapter5/Example22.java
Normal file
@ -0,0 +1,30 @@
|
||||
package chapter5;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class Example22 {
|
||||
public static void main(String[] args) {
|
||||
// 1. 获取当前时间
|
||||
LocalTime now = LocalTime.now();
|
||||
// 2. 表示过去时间
|
||||
LocalTime of = LocalTime.of(9, 23, 23);
|
||||
// 3. 三个getter
|
||||
System.out.println("时:" + now.getHour());
|
||||
System.out.println("分:" + now.getMinute());
|
||||
System.out.println("秒:" + now.getSecond());
|
||||
|
||||
// 4. 格式转换,借助 DateTimeFormatter
|
||||
System.out.println("\nnow默认格式:" + now);
|
||||
System.out.println("不显示微秒:" + now.withNano(0));
|
||||
System.out.println("转换格式:" + now.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
|
||||
System.out.println("转换格式:" + now.format(DateTimeFormatter.ofPattern("HH点mm分ss秒")));
|
||||
|
||||
// 5. 判断
|
||||
System.out.println("\nof在now之前吗?" + of.isBefore(now));
|
||||
|
||||
// 6. 解析字符串
|
||||
LocalTime time = LocalTime.parse("12:15:30");
|
||||
System.out.println("将字符串转换成时间对象:" + time);
|
||||
}
|
||||
}
|
4
src/chapter5/Example23.java
Normal file
4
src/chapter5/Example23.java
Normal file
@ -0,0 +1,4 @@
|
||||
package chapter5;
|
||||
|
||||
public class Example23 {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user