当前位置: 代码迷 >> Java相关 >> 计算世界末日倒数日
  详细解决方案

计算世界末日倒数日

热度:380   发布时间:2010-03-03 10:27:05.0
计算世界末日倒数日
看了电影《2012》,编写一个程序计算现在距离世界末日还有多少天

import java.util.*;

public class Date2010 {
    public static void main(String[] args) {
        GregorianCalendar g = new GregorianCalendar();
/*        int year = (int)g.get(Calendar.YEAR);
        int month = (int)g.get(Calendar.MONTH)+1;
        int date = (int)g.get(Calendar.DATE);
        System.out.println("Today is " + year + "-" + month + "-" + date);
*/        
        GregorianCalendar f = new GregorianCalendar(2012, Calendar.DECEMBER, 21);
        
        long dMillis = f.getTimeInMillis() - g.getTimeInMillis();
        long dDay = dMillis/(24*60*60*1000);
        
        System.out.println("今天距离2012年12月21日还有"+dDay+"日");
    }
}
搜索更多相关的解决方案: 世界  末日  

----------------解决方案--------------------------------------------------------
有没有界面化啊

----------------解决方案--------------------------------------------------------
世界末日
你还真相信?无科学根据吧...
----------------解决方案--------------------------------------------------------
以下是引用xjwxj526在2010-3-3 11:12:32的发言:

有没有界面化啊
import java.util.*;
import java.awt.*;
import javax.swing.*;


public class Date2010 extends JFrame{
   
    private JLabel lbl;
    private GregorianCalendar g;
    private GregorianCalendar f;
    private long dMillis;
    private long dDay;
   
    public Date2010() {
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(200, 200, 650, 100);
        lbl = new JLabel();
        lbl.setBounds(0,0,650,100);
        lbl.setFont(new Font(null, Font.BOLD, 30));
        lbl.setForeground(Color.red);

        g = new GregorianCalendar();  
        f = new GregorianCalendar(2012, Calendar.DECEMBER, 21);   
        dMillis = f.getTimeInMillis() - g.getTimeInMillis();
        dDay = dMillis/(24*60*60*1000);

        lbl.setText("今天距离2012年12月21日还有"+dDay+"日");
        this.add(lbl);
        this.setTitle("2012");
        this.setVisible(true);

    }
   
    public static void main(String[] args) {
        Date2010 date2010 = new Date2010();
    }
}
----------------解决方案--------------------------------------------------------
一万年以后你将被载入史册
----------------解决方案--------------------------------------------------------
时分秒计时呢
----------------解决方案--------------------------------------------------------
纯属娱乐
----------------解决方案--------------------------------------------------------
。。。
----------------解决方案--------------------------------------------------------
以下是引用心无旁骛在2010-3-7 00:08:17的发言:

时分秒计时呢
import java.util.*;
import java.awt.*;
import javax.swing.*;


public class Date2010_1 extends JFrame implements Runnable{
   
    private JLabel lbl;
    private GregorianCalendar g;
    private GregorianCalendar f;
    private long dMillis;
    private long dDay;
    private long dHour;
    private long dMin;
    private long dSec;
   
    public Date2010_1() {
        this.setLayout(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(200, 200, 800, 100);
        lbl = new JLabel();
        lbl.setBounds(0,0,800,100);
        lbl.setFont(new Font(null, Font.BOLD, 30));
        lbl.setForeground(Color.red);

        this.add(lbl);
        this.setTitle("2012");
        this.setVisible(true);

    }
   
  public void run() {
      while(true) {
          try {
          g = new GregorianCalendar();  
        f = new GregorianCalendar(2012, Calendar.DECEMBER, 21,0,0,0);   
        dMillis = f.getTimeInMillis() - g.getTimeInMillis();
        dDay = dMillis/(24*60*60*1000);
        dHour = 24 - g.get(Calendar.HOUR_OF_DAY);
        dMin = 60 - g.get(Calendar.MINUTE) ;
        dSec = 60 - g.get(Calendar.SECOND) ;
        String str = "今天距离2012年12月21日还有"+dDay+"日" + dHour + "小时" +dMin +"分钟" + dSec + "秒";
        lbl.setText(str);
   
        Thread.sleep(1000);
        } catch (Exception ex) {
        }

      }
  }
   
    public static void main(String[] args) {
        Date2010_1 date2010 = new Date2010_1();
        Thread th = new Thread(date2010);
        th.start();
    }
}
----------------解决方案--------------------------------------------------------
你写一个你什么时候去见佛主的嘛。
----------------解决方案--------------------------------------------------------
  相关解决方案