java生存时间统计:1.用户输入出生的年月日 2.程序会计算的出已经生存的时间 3.生存时间分别以年月日时分秒和毫秒为单位统计
时间: 2024-03-02 17:48:52 浏览: 128
好的,我可以为您编写这个 Java 程序。下面是代码实现:
```java
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class LifetimeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入您的出生年份:");
int year = scanner.nextInt();
System.out.print("请输入您的出生月份:");
int month = scanner.nextInt();
System.out.print("请输入您的出生日期:");
int day = scanner.nextInt();
LocalDate birthDate = LocalDate.of(year, month, day);
LocalDate now = LocalDate.now();
long years = ChronoUnit.YEARS.between(birthDate, now);
long months = ChronoUnit.MONTHS.between(birthDate.plusYears(years), now);
long days = ChronoUnit.DAYS.between(birthDate.plusYears(years).plusMonths(months), now);
long hours = ChronoUnit.HOURS.between(birthDate.atStartOfDay().plusYears(years).plusMonths(months).plusDays(days), LocalDateTime.now());
long minutes = ChronoUnit.MINUTES.between(birthDate.atStartOfDay().plusYears(years).plusMonths(months).plusDays(days).plusHours(hours), LocalDateTime.now());
long seconds = ChronoUnit.SECONDS.between(birthDate.atStartOfDay().plusYears(years).plusMonths(months).plusDays(days).plusHours(hours).plusMinutes(minutes), LocalDateTime.now());
long millis = ChronoUnit.MILLIS.between(birthDate.atStartOfDay().plusYears(years).plusMonths(months).plusDays(days).plusHours(hours).plusMinutes(minutes).plusSeconds(seconds), LocalDateTime.now());
System.out.println("您已经生存了:" + years + "年" + months + "月" + days + "日" + hours + "时" + minutes + "分" + seconds + "秒" + millis + "毫秒");
}
}
```
您可以复制以上代码并粘贴到您的 Java IDE 中运行。程序会要求用户输入出生年月日,并计算出已经生存的时间,包括年月日时分秒和毫秒。
阅读全文