Java怎么实现倒计时时分秒
发表于:2024-10-27 作者:千家信息网编辑
千家信息网最后更新 2024年10月27日,这篇文章主要讲解了"Java怎么实现倒计时时分秒",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java怎么实现倒计时时分秒"吧!package com
千家信息网最后更新 2024年10月27日Java怎么实现倒计时时分秒
这篇文章主要讲解了"Java怎么实现倒计时时分秒",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java怎么实现倒计时时分秒"吧!
package com.test;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Timer;import java.util.TimerTask;/** * 多任务定时器 * * */public class Testsss { public final static Mapmap = new HashMap<>(); public static void main(String[] args) { String nowdate = "2019-09-09 10:28:17"; String startdate = "2019-09-09 10:28:20"; Date nowdate1 = parseToDateTime(nowdate,"yyyy-MM-dd HH:mm:ss"); Date startdate1 = parseToDateTime(startdate,"yyyy-MM-dd HH:mm:ss"); String nowdate2 = "2019-09-09 10:20:17"; String startdate2 = "2019-09-09 10:20:21"; Date nowdate12 = parseToDateTime(nowdate2,"yyyy-MM-dd HH:mm:ss"); Date startdate12 = parseToDateTime(startdate2,"yyyy-MM-dd HH:mm:ss"); int deff = getBetweenTimes(nowdate1,startdate1); System.out.println(deff); int deff2 = getBetweenTimes(nowdate12,startdate12); System.out.println(deff2); map.put("id1", String.valueOf(deff)); map.put("id2", String.valueOf(deff2)); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("timer ...."); Iterator entries = map.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = (Map.Entry) entries.next(); String taskId = (String)entry.getKey(); String time = (String)entry.getValue(); int times = Integer.parseInt(time); if(times>=0) { times--; map.put(taskId,String.valueOf(times)); System.out.println(taskId+"->"+framt(Integer.parseInt(time))); }else { map.remove(taskId); } if(map.size()==0) { System.out.println("done"); timer.cancel(); } } } },0,1000); } /** * 秒转时分秒 * * @param seconds */ public static String framt(int seconds) { int temp=0; StringBuffer sb=new StringBuffer(); temp = seconds/3600; sb.append((temp<10)?"0"+temp+":":""+temp+":"); temp=seconds%3600/60; sb.append((temp<10)?"0"+temp+":":""+temp+":"); temp=seconds%3600%60; sb.append((temp<10)?"0"+temp:""+temp); return sb.toString(); } /** * 将字符串转换为指定格式的日期时间 * * @param strDate * 日期字符串 * @param pattern * 日期格式,与strDate格式匹配 */ public static Date parseToDateTime(String strDate, String pattern) { try { return new SimpleDateFormat(pattern).parse(strDate); } catch (ParseException e) { e.printStackTrace(); } return null; } /** * 时间差 秒 * */ public static int getBetweenTimes(Date start, Date end) { long times = end.getTime() - start.getTime(); return (int) times / 1000; }}
感谢各位的阅读,以上就是"Java怎么实现倒计时时分秒"的内容了,经过本文的学习后,相信大家对Java怎么实现倒计时时分秒这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
时分
倒计时
日期
格式
学习
内容
字符
字符串
时间
任务
定时器
就是
思路
情况
文章
时间差
更多
知识
知识点
篇文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
香港恒生互联网科技指数简介
sql查看对应软件的数据库
阿里云服务器购买企业聊天系统
软件开发岗和研发岗的区别
服务器域搭建与管理
一个服务器有几个固定ip
wowptr服务器怎么进
聊天软件开发开题报告
任天堂服务器没有保存数据
喻志彬 信息网络安全
花都网络安全运维
简述设计数据库的重要性
蜘蛛网络安全团队杜伟
软件开发解决方案推荐
对yggl数据库完成查询
服务器选型配置方案
常州大型软件开发哪家好
东莞支付软件开发费用是多少
工商银行 软件开发 社招
西门子数据库
软件开发包括为那几个部分
c是什么控件可以从数据库下载
旅游系统数据库
萤石云服务器怎么样
wowptr服务器怎么进
云服务器上安装应用
怎么查询服务器是否连接外网
洪山区营销网络安全维护代理品牌
高程数据库
灌篮高手下载软件开发