您的位置:首页 > 其它

三星题:(Current date and time)

2015-10-20 17:44 253 查看
问题及代码:

public class Get_the_data {
public static void main(String[] args) {

int year = 1970, month = 1, days, hours, minutes;
long Get_second = (long) (System.currentTimeMillis() / 1000);      //取出0区的时间

// 一般的 年 月 日的时间
int Year_sec = 365 * 24 * 60 * 60;
int[] Month_day = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int Day_sec = 24 * 60 * 60;
int Hour_sec = 60 * 60;
int Minute_sec = 60;

while (Get_second >= Year_sec) // 算出年数
{
year++;
Get_second -= Year_sec;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
Get_second -= Day_sec;
}
}

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) // 确定现在是否是闰年
Month_day[2] = 29;

int i = 1;
while (Get_second >= Day_sec * Month_day[i]) // 算出月份
{
month++;
Get_second -= Day_sec * Month_day[i];
i++;
}

String Months;
switch (month) {
case 1:
Months = "January";
break;
case 2:
Months = "February";
break;
case 3:
Months = "March";
break;
case 4:
Months = "April";
break;
case 5:
Months = "May";
break;
case 6:
Months = "June";
break;
case 7:
Months = "Jule";
break;
case 8:
Months = "August";
break;
case 9:
Months = "September";
break;
case 10:
Months = "October";
break;
case 11:
Months = "November";
break;
case 12:
Months = "December";
break;
default:
Months = "Error!";
break;
}

days = (int) (Get_second / Day_sec + 1); // 确定现在的天
Get_second %= Day_sec;

hours = (int) (Get_second / Hour_sec+8); // 确定现在的小时(北京时间)
Get_second %= Hour_sec;

minutes = (int) (Get_second / Minute_sec); // 确定日期的分钟

Get_second %= Minute_sec; // 确定日期的秒数

System.out.print("Current date and time is " + Months + " " + days
+ ", " + year + "   " + hours + ":" + minutes + ":"
+ Get_second);
}
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: