java生存时间统计:. 1.用户输入出生的年月日 2.程序会计算的出已经生存的时间 3.生存时间分别以年月日时分秒和毫秒为单位统计
时间: 2023-05-22 08:05:54 浏览: 194
好的,根据您的要求,我作为 ChitGPT,可以回答您的编程问题。
以下是 Java 生存时间统计的代码示例:
```
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Period;
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);
LocalDateTime birthTime = LocalDateTime.of(birthDate, LocalTime.of(0, 0, 0));
LocalDateTime currentTime = LocalDateTime.now();
Period period = Period.between(birthDate, currentTime.toLocalDate());
long totalTimeMillis = ChronoUnit.MILLIS.between(birthTime, currentTime);
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
int hours = (int) (totalTimeMillis / (1000*60*60));
int minutes = (int) (totalTimeMillis / (1000*60)) % 60;
int seconds = (int) (totalTimeMillis / 1000) % 60;
long millis = totalTimeMillis % 1000;
System.out.println("您已经生存了:" + years + " 年 " + months + " 个月 " + days + " 天 " + hours + " 小时 " + minutes + " 分钟 " + seconds + " 秒 " + millis + " 毫秒");
}
}
```
您可以在控制台输入您的出生年月日,程序将计算出您已经生存的时间,并以年月日时分秒和毫秒为单位进行统计。
阅读全文