生命钟表

生命钟表

前几天在进行所谓的算命的时候,我想到了是否自己可以为自己制作一个生命钟表,看看自己在这个世界上到底待机多少天?

理论依据

  • 用户输入自己的时间
  • 可以通过无限循环来保证程序的执行、
  • 通过java语言中的Date类来获得一个当地时间
  • 系统时间减去出生时间就是我的待机时间

代码实践

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.dwx;

import java.sql.Time;
import java.util.Date;
import java.util.Scanner;

public class LifeClock {
public static void main(String[] args) {
//记录个人的出生日期
//输入时间
int MyYear;
int MyMonth;
int MyDate;
int MyHour;
int MyMin;
int MySecond;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的生日(不知道的数据填0)");
System.out.println("格式为(yy mm dd h m s)");
MyYear = scanner.nextInt();
MyMonth = scanner.nextInt();
MyDate = scanner.nextInt();
MyHour = scanner.nextInt();
MyMin = scanner.nextInt();
MySecond = scanner.nextInt();

Date OldDate = new Date(MyYear-1900, MyMonth, MyDate,MyHour,MyMin,MySecond);


//两者的差算作年龄
//通过年份相差获得年
//通过月份相差获得月
//通过Day相差获得天
//****
//规范化这个过程
while(true){
//获得现在的时间
Date NowDate = new Date();


int year;
int month;
int day;
int hour;
int min;
int sed;

year = NowDate.getYear()-OldDate.getYear();
month = NowDate.getMonth()-OldDate.getMonth();
day = NowDate.getDay()-OldDate.getDay();
hour = NowDate.getHours()-OldDate.getHours();
min = NowDate.getMinutes()-OldDate.getMinutes();
sed = NowDate.getSeconds()-OldDate.getSeconds();

//对代码健壮性进行处理
if (sed < 0){
min= min-1;
sed = sed+60;
}
if (min<0){
hour = hour-1;
min = min + 60;
}
if (hour<0){
hour = hour+24;
day = day-1;
}

if (day<0){
month=month-1;
day = day+30;
}
if (month<0){
year=year-1;
month = month+12;
}
if (year<0){
System.out.println("请查看您输入的出生日期!!!");
break;
}
//输出计算结果
System.out.print(year+"年");
System.out.print(month+"月");
System.out.print(day+"日");
System.out.print(hour+"时");
System.out.print(min+"分");
System.out.println(sed+"秒");
try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}

}
}

效果图

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2015-2023 dwx
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信