从未来某个时间到现在,实现一个小的倒计时功能。

  • 首先获取当前时间:
方法一:通过Util包的Calendar 获取时间,分别获取年月日时分秒
	Calendar rightNow = Calendar.getInstance(); // 记录现在时间
	int y = rightNow.get(Calendar.YEAR);
	int m = rightNow.get(Calendar.MONTH) + 1;
	int d = rightNow.get(Calendar.DATE);
	int h = rightNow.get(Calendar.HOUR_OF_DAY);
	int mi = rightNow.get(Calendar.MINUTE);
	int s = rightNow.get(Calendar.SECOND);
	System.out.println("现在时间是" + y + "年" + m + "月" + d + "日" + h + "时" + mi + "分" + s + "秒"); 

方法二:通过Util包中的Date获取
	Date date = new Date();
	SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
	System.out.println(dateFormat.format(date));
	
方法三:通过Util包的Calendar 获取
	Calendar calendar= Calendar.getInstance();
	SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
	System.out.println(dateFormat.format(calendar.getTime()));
   
  • 接着输入未来倒计时时间
	Calendar cal = Calendar.getInstance();   //设置倒计时时间
 	System.out.println("The current date is : " + cal.getTime());
  	cal.clear();
   	cal.set(2020, 10, 31, 12, 30, 00);
   	System.out.println("New date is : " + cal.getTime());
  • 然后实现倒计时功能
 public static String countDown(Calendar endDate,Calendar nowDate) {


            long nd = 1000 * 24 * 60 * 60;
            long nh = 1000 * 60 * 60;
            long nm = 1000 * 60;
            // long ns = 1000;
            // 获得两个时间的毫秒时间差值
            long times = endDate.getTimeInMillis() - nowDate.getTimeInMillis();
            // 计算差多少天
            long day = times/ nd;
            // 计算差多少小时
            long hour = times % nd / nh;
            // 计算差多少分钟
            long min = times % nd % nh / nm;
            // 计算差多少秒//输出结果
            // long sec = times % nd % nh % nm / ns;
            return day + "天," + hour + "小时," + min + "分钟,";
        }
  • 最后实现实时刷新(每隔一段时间刷新)
final Thread threadService = new Thread();
        Runnable runnable = new Runnable() {
            public void run() {
                while (true) {
                    Calendar rightNow = Calendar.getInstance();
                    System.out.println("距离" + cal.getTime() + "   还有" + countDown(cal, rightNow));
                    System.out.println("============================================================");

                    try {
                        Thread.sleep(1000);// 1秒运行一次
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();

用到的思想方法:
1、输入某个日期(年月日时分秒)

将字符串类型的时间转换成date类型可以使用SimpleDateFormat来转换,具体方法如下:
1、定义一个字符串类型的时间;
2、创建一个SimpleDateFormat对象并设置格式;
3、最后使用SimpleDateFormat的parse方法将String类型的时间转换成Date类型的时间。
具体代码如下:
String string = "2014-3-17";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
		date = dateFormat.parse(string);
		System.out.println(date.toLocaleString().split(" ")[0]);//切割掉不要的时分秒数据
	} catch (ParseException e) {
	e.printStackTrace();
}

2、从键盘输入某个日期

//第一种方法
	Scanner input = new Scanner(System.in);
	System.out.print("请输入:");
	Date date = simpleDateFormat.parse(input.nextLine());
	Calendar calendar=Calendar.getInstance();
	calendar.setTime(date);
//第二种方法
	Scanner in = new Scanner(System.in);
    System.out.println("请输入日期(xxxx-xx-xx xx:xx:xx):");
    String inTime = in.nextLine();        // 未校验
    System.out.println("与当前时间毫秒差为:" + funcTime(inTime));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值