输入2011年3月某一天的某一时刻,在屏幕上输出1970年1月1日至该时刻的毫秒数。...

/*
 *题目描述
输入2011年3月某一天的某一时刻,在屏幕上输出1970年1月1日至该时刻的毫秒数。

输入描述
用"-",空格及":"分隔的日期时间值。

输出描述
“毫秒数:”+整数值

输入样例
2011-03-11 15:00:00

输出样例
毫秒数:12345678

 *
 *
 * 四年一闰;百年不闰,四百年再闰
 */

package exam1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author Administrator
 */
public class Test2 {
    String endTime;
    long haomaio;

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime() throws IOException {
        BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
        String time = br.readLine();
        this.endTime = time;
    }

    public long getHaomaio() {
        return haomaio;
    }

    public void setHaomaio(String endTime) {
        long temp=0;
        String date[] =endTime.split("[- :]");

        int year=Integer.parseInt(date[0]);
        int month=Integer.parseInt(date[1]);
        int day=Integer.parseInt(date[2]);
        int h=Integer.parseInt(date[3]);
        int m=Integer.parseInt(date[4]);
        int s=Integer.parseInt(date[5]);
        //System.out.println(year+" "+month+" "+day+" "+h+" "+m+" "+s);
        int t0=h*60*60*60+m*60*60+s*1000;
        int t1=28*24*60*60*1000;
        int t2=29*24*60*60*1000;
        int t3=30*24*60*60*1000;
        int t4=31*24*60*60*1000;
        int t5=(day-1)*24*60*60*1000;

        for(int i=1970;i<year;i++){
            if(isRYear(i)){
                temp+=366*24*60*60*1000;
            }else temp+=365*24*60*60*1000;
        }
        if(isRYear(year)){
          if(month==1){
            temp+=t5+t0;
           }else if(month==2){
               temp+=t4+t5+t0;
           }else if(month==3){
               temp+=t5+t4+t2+t0;
           }else if(month==4){
               temp+=t5+t4*2+t2+t0;
           }else if(month==5){
               temp+=t5+t4*2+t3+t2+t0;
           }else if(month==6){
               temp+=t5+t4*3+t3+t2+t0;
           }else if(month==7){
                temp+=t5+t4*3+t3*2+t2+t0;
           }else if(month==8){
                temp+=t5+t4*4+t3*2+t2+t0;
           }else if(month==9){
                temp+=t5+t4*5+t3*2+t2+t0;
           }else if(month==10){
                temp+=t5+t4*5+t3*3+t2+t0;
           }else if(month==11){
                temp+=t5+t4*6+t3*3+t2+t0;
           }else if(month==12){
                temp+=t5+t4*6+t3*4+t2+t0;
           }
        }else {
            if(month==1){
            temp+=t5+t0;
           }else if(month==2){
               temp+=t4+t5+t0;
           }else if(month==3){
               temp+=t5+t4+t1+t0;
           }else if(month==4){
               temp+=t5+t4*2+t1+t0;
           }else if(month==5){
               temp+=t5+t4*2+t3+t1+t0;
           }else if(month==6){
               temp+=t5+t4*3+t3+t1+t0;
           }else if(month==7){
                temp+=t5+t4*3+t3*2+t1+t0;
           }else if(month==8){
                temp+=t5+t4*4+t3*2+t1+t0;
           }else if(month==9){
                temp+=t5+t4*5+t3*2+t1+t0;
           }else if(month==10){
                temp+=t5+t4*5+t3*3+t1+t0;
           }else if(month==11){
                temp+=t5+t4*6+t3*3+t1+t0;
           }else if(month==12){
                temp+=t5+t4*6+t3*4+t1+t0;
           }

        }
        this.haomaio = temp;
    }

    public boolean isRYear(int year){
        boolean isRYear=false;
        if(year%4==0&&year%100!=0||year%400==0)
            isRYear=true;
        return isRYear;
    }

    public static void main(String[] args) throws IOException {
        Test2 test= new Test2();
        test.setEndTime();
        test.setHaomaio(test.getEndTime());
        System.out.println("毫秒数:"+test.getHaomaio());
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值