用java计算天数_java 如何算还剩多少天数,小时数,之类的

展开全部

import java.awt.BorderLayout;

import java.awt.FlowLayout;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class Tester extends JFrame

{

private static final long serialVersionUID = 1L;

private static int DELAY = 0;

private static int PERIOD = 1000;

private static final String DATE_TIME = "2015年4月19日";

private static final String INFO = "距 " + DATE_TIME + " 有: 
";

private long that;

private JLabel label;

public Tester ()

{

setTitle ("倒计时牌32313133353236313431303231363533e4b893e5b19e31333337613136");

setResizable (false);

setLayout (new BorderLayout ());

setSize (200, 100);

setLocationRelativeTo (null);

setAlwaysOnTop (true);

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

}

private Tester runTimer ()

{

Timer timer = new Timer ();

timer.schedule (new TimerTask ()

{

@Override

public void run ()

{

long ms = that - new Date ().getTime ();

int day = (int) ( ms / 1000 / 60 / 60 / 24 );

int hour = (int) ( ms / 1000 / 60 / 60 % 24 );

int minute = (int) ( ms / 1000 / 60 % 60 );

int second = (int) ( ms / 1000 % 60 );

label.setText ("
" + INFO + "" + day + " 天 " + hour + " 时 " + minute + " 分 " + second + " 秒."

+ "");

}

}, DELAY, PERIOD);

return this;

}

private Tester init ()

{

String format = "";

if (DATE_TIME.matches ("^\\d+年$"))

{

format = "yyyy年";

}

else if (DATE_TIME.matches ("\\d+年\\d+月"))

{

format = "yyyy年MM月";

}

else if (DATE_TIME.matches ("\\d+年\\d+月\\d+日"))

{

format = "yyyy年MM月dd日";

}

SimpleDateFormat sdf = new SimpleDateFormat (format);

try

{

that = sdf.parse (DATE_TIME).getTime ();

}

catch (ParseException e)

{

e.printStackTrace ();

}

label = new JLabel ();

JPanel panel = new JPanel (new FlowLayout (FlowLayout.CENTER));

panel.add (label);

add (panel, BorderLayout.CENTER);

return this;

}

public static void main ( String[] args )

{

SwingUtilities.invokeLater (new Runnable ()

{

@Override

public void run ()

{

new Tester ().init ().runTimer ().setVisible (true);

}

});

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值