Compare commits
No commits in common. "6da1f053fb0cd005966ca2fb346cacf751053858" and "9c663672aa81622e41bd4c2ff403f8e5d04c8ceb" have entirely different histories.
6da1f053fb
...
9c663672aa
@ -1,19 +1,4 @@
|
|||||||
package chapter5;
|
package chapter5;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
|
|
||||||
public class Example23 {
|
public class Example23 {
|
||||||
public static void main(String[] args) {
|
|
||||||
// 1. 获取当前时间
|
|
||||||
LocalDateTime now = LocalDateTime.now();
|
|
||||||
System.out.println("原格式:" + now);
|
|
||||||
System.out.println("只有日期:" + now.toLocalDate());
|
|
||||||
System.out.println("只有时间:" + now.toLocalTime());
|
|
||||||
// 2. getter 方法
|
|
||||||
System.out.println(now.getYear() +"年" + now.getMonthValue() + "月" + now.getDayOfMonth() + "号 "
|
|
||||||
+ now.getHour() + "点" + now.getMinute() + "分" + now.getSecond() + "秒");
|
|
||||||
// 3. 指定格式
|
|
||||||
System.out.println("指定格式:" + now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.time.LocalTime;
|
|
||||||
|
|
||||||
public class Example24 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
LocalTime start = LocalTime.now();
|
|
||||||
LocalTime end = LocalTime.of(21, 53, 0);
|
|
||||||
|
|
||||||
Duration duration = Duration.between(start, end);
|
|
||||||
|
|
||||||
System.out.println("时间间隔(纳秒)" + duration.toNanos());
|
|
||||||
System.out.println("时间间隔(毫秒)" + duration.toMillis());
|
|
||||||
System.out.println("时间间隔(秒)" + duration.toSeconds());
|
|
||||||
System.out.println("时间间隔(分钟)" + duration.toMinutes());
|
|
||||||
System.out.println("时间间隔(小时)" + duration.toHours());
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.Period;
|
|
||||||
|
|
||||||
public class Example25 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
LocalDate birthday = LocalDate.of(2018, 12, 12);
|
|
||||||
LocalDate now = LocalDate.of(2020, 11, 13);
|
|
||||||
|
|
||||||
Period period = Period.between(birthday, now);
|
|
||||||
System.out.println("时间间隔(年)" + period.getYears());
|
|
||||||
System.out.println("时间间隔(月)" + period.getMonths());
|
|
||||||
System.out.println("时间间隔(天)" + period.getDays());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,43 +0,0 @@
|
|||||||
package chapter5;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class demo55 {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Scanner scn = new Scanner(System.in);
|
|
||||||
System.out.println("请输入要判断的年份:");
|
|
||||||
int year = scn.nextInt();
|
|
||||||
|
|
||||||
System.out.println("方法一:" + year + "的2月有" + getNumOfDaysInFeb1(year) + "天");
|
|
||||||
System.out.println("方法二:" + year + "的2月有" + getNumOfDaysInFeb2(year) + "天");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 思路一
|
|
||||||
* @param year 年份
|
|
||||||
* @return 2月的天数
|
|
||||||
*/
|
|
||||||
public static int getNumOfDaysInFeb1(int year) {
|
|
||||||
LocalDate date = LocalDate.of(year, 3, 1);
|
|
||||||
LocalDate feb = date.minusDays(1);
|
|
||||||
return feb.getDayOfMonth();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 思路二
|
|
||||||
* @param year 年份
|
|
||||||
* @return 2月的天数
|
|
||||||
*/
|
|
||||||
public static int getNumOfDaysInFeb2(int year) {
|
|
||||||
LocalDate date = LocalDate.of(year, 1, 1);
|
|
||||||
if (date.isLeapYear()) {
|
|
||||||
return 29;
|
|
||||||
} else {
|
|
||||||
return 28;
|
|
||||||
}
|
|
||||||
|
|
||||||
// return date.isLeapYear() ? 29 : 28;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user