java三星题之_Java三星题之显示日期

题目:

Invoking System.currentTimeMillis() return the elapse time in milliseconds since midnight of January 1 ,1970.write a program that displays the date and time。

题目来源:

题目选自《JAVA程序语言设计》 P196-5.33***

解题思路:

1.调用System.currentTimeMillis()函数来获得1970年1月1日距今的毫秒数,并通过除以1000将单位化成秒。

2.确定一年的秒数,每一天的秒数,每一个小时的秒数,每一分钟的秒数。

3.首先通过循环一次次的用总秒数减去一年的秒数(闰年则多减一天)直至剩余秒数比一年的秒数小,则可以确定当前年份。

4.然后通过数组将每一个月的天数列出(13项,第一项为0.否则后面的i需要减去1) 然后用剩余时间依次按照月份减去月份的天数乘以一天的时间,直至剩余时间比当前月份的总秒数小,则可以确定当前月数,然后利用switch语句和String将月份转换为英文格式。

5.随后利用剩余的时间除以一天的秒数,确定当前时间是本月的几号(由于是从一号开始,所以天数要加一)。

6.用除一天的秒数取余后的剩余时间除以一小时的秒数来确定当前时间的小时。

7.再用除一小时的秒数取余后的剩余时间除以一分钟的秒数来确定当前时间的分钟。

8.最后剩余时间除一分钟的秒数取余数,即当前时间的秒数。

代码如下:

import java.util.*;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

long Msec=(long)(System.currentTimeMillis()/1000); //确定1970年1月1日距今的总秒数

int year = 1970 , month = 1,days , hours , minutes ;

int[] day = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

long YearSec = 365 * 24 * 60 * 60; //一年的秒数

long DaySec = 24 * 60 * 60; //一天的秒数

long HourSec = 60 * 60; //一小时的秒数

long MinuteSec = 60 ; //一分钟的秒数

long Second = 0;

while ( Msec - Second >= YearSec )//如果总秒数减去已统计过的秒数大于一年的秒数,继续循环

{

Second += YearSec; //统计年份之差秒数

if (( year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 ))

{

Second += DaySec; //如果某一年是闰年则多加一天的秒数

}

year++;

}

Second = Msec - Second; //剩余时间=总秒数-之前的年份的总秒数

int i=1;

if (( year % 4 == 0 && year % 100 != 0) || year % 400 == 0 )//确定年份后如果今年是闰年则2月为29天,否则不变

day[2] = 29;

while ( Second >= DaySec*day[i]) //如果剩余秒数大于第i月的秒数,继续循环

{

month++; //确定月份

Second -= DaySec*day[i]; //剩余秒数减去统计过的第i月的秒数

i++; //每进行一次统计.月份加1,且不用担心超过12月。

}

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)(Second / DaySec); //确定日期的天

Second %= DaySec;

days += 1; //天数从1号开始计算,所以加1

hours = (int)(Second / HourSec); //确定日期的小时

Second %= HourSec;

minutes = (int)(Second/ MinuteSec);//确定日期的分

Second %= MinuteSec;//确定日期的秒

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

}

}

运行结果:

/*output:

* Current date and time is October 13 2015 11:51:38

*///~

(2)函数解决问题

import java.util.Date;

import java.text.SimpleDateFormat;

public class Main{

public static void main(String[] args){

Date now = new Date();

System.out.println(now);

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

System.out.println(df.format(new Date()));// new Date()为获取当前系统时间

}

}

运行结果:

/*output:

* Tue Oct 13 19:53:43 CST 2015

* Current date and time is 2015-10-13 19:53:43

*///~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值