用java实现生存时间统计: 1. 用户输入出生的年月日2.程序会计算的出已经生存的时间。 3.生存时间分别以年月日时分秒和毫秒为单位统计。
时间: 2023-05-28 11:03:20 浏览: 121
以下是用Java实现生存时间统计的代码:
```java
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class AgeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的出生年月日(格式:yyyy-mm-dd):");
String input = scanner.nextLine();
scanner.close();
LocalDate birthday = LocalDate.parse(input);
LocalDate today = LocalDate.now();
Period period = Period.between(birthday, today);
long days = ChronoUnit.DAYS.between(birthday, LocalDateTime.now());
long hours = ChronoUnit.HOURS.between(birthday.atStartOfDay(), LocalDateTime.now());
long minutes = ChronoUnit.MINUTES.between(birthday.atStartOfDay(), LocalDateTime.now());
long seconds = ChronoUnit.SECONDS.between(birthday.atStartOfDay(), LocalDateTime.now());
long milliseconds = ChronoUnit.MILLIS.between(birthday.atStartOfDay(), LocalDateTime.now());
System.out.println("您已经生存了 " + period.getYears() + " 年 " + period.getMonths() + " 个月 " + period.getDays() + " 天。");
System.out.println("您已经生存了 " + days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 " + milliseconds + " 毫秒。");
}
}
```
运行程序后,会提示用户输入出生年月日,然后计算出已经生存的时间,并以年月日时分秒和毫秒为单位进行统计。最后将结果输出。
阅读全文