[Java]给指定时间加上十秒
package com.testEmp; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class PrintTime { public static void main(String[] args) { String currTime="2018-01-01 00:00:00"; for(int i=0;i<3200000;i++) { System.out.println(currTime); currTime=timePastTenSecond(currTime); } } public static String timePastTenSecond(String otime) { try { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt=sdf.parse(otime); Calendar newTime = Calendar.getInstance(); newTime.setTime(dt); newTime.add(Calendar.SECOND,10);//日期加10秒 Date dt1=newTime.getTime(); String retval = sdf.format(dt1); return retval; } catch(Exception ex) { ex.printStackTrace(); return null; } } }
输出:
2018-01-01 18:46:10 2018-01-01 18:46:20 2018-01-01 18:46:30 2018-01-01 18:46:40 2018-01-01 18:46:50 2018-01-01 18:47:00 2018-01-01 18:47:10 2018-01-01 18:47:20 2018-01-01 18:47:30 2018-01-01 18:47:40 2018-01-01 18:47:50 2018-01-01 18:48:00 2018-01-01 18:48:10 2018-01-01 18:48:20 2018-01-01 18:48:30 2018-01-01 18:48:40 2018-01-01 18:48:50 2018-01-01 18:49:00 2018-01-01 18:49:10 2018-01-01 18:49:20 2018-01-01 18:49:30 2018-01-01 18:49:40 2018-01-01 18:49:50 2018-01-01 18:50:00 2018-01-01 18:50:10 2018-01-01 18:50:20 2018-01-01 18:50:30 2018-01-01 18:50:40 2018-01-01 18:50:50 2018-01-01 18:51:00 2018-01-01 18:51:10 2018-01-01 18:51:20 2018-01-01 18:51:30 2018-01-01 18:51:40 2018-01-01 18:51:50 2018-01-01 18:52:00 2018-01-01 18:52:10 2018-01-01 18:52:20 ... 2018-01-01 18:55:50 2018-01-01 18:56:00 2018-01-01 18:56:10 2018-01-01 18:56:20 2018-01-01 18:56:30 ... 2018-01-01 19:12:00 2018-01-01 19:12:10 2018-01-01 19:12:20 2018-01-01 19:12:30 2018-01-01 19:12:40 2018-01-01 19:12:50 2018-01-01 19:13:00 2018-01-01 19:13:10 2018-01-01 19:13:20 2018-01-01 19:13:30 2018-01-01 19:13:40 2018-01-01 19:13:50 2018-01-01 19:14:00 2018-01-01 19:14:10 2018-01-01 19:14:20 2018-01-01 19:14:30 2018-01-01 19:14:40 2018-01-01 19:14:50 2018-01-01 19:15:00 2018-01-01 19:15:10 2018-01-01 19:15:20 ... 2018-01-01 19:19:00 2018-01-01 19:19:10 2018-01-01 19:19:20 2018-01-01 19:19:30 2018-01-01 19:19:40 2018-01-01 19:19:50 2018-01-01 19:20:00 2018-01-01 19:20:10 2018-01-01 19:20:20 2018-01-01 19:20:30 2018-01-01 19:20:40 2018-01-01 19:20:50 2018-01-01 19:21:00 2018-01-01 19:21:10 2018-01-01 19:21:20 2018-01-01 19:21:30 2018-01-01 19:21:40 2018-01-01 19:21:50 2018-01-01 19:22:00 2018-01-01 19:22:10 2018-01-01 19:22:20 2018-01-01 19:22:30 2018-01-01 19:22:40 2018-01-01 19:22:50 2018-01-01 19:23:00 2018-01-01 19:23:10 2018-01-01 19:23:20 2018-01-01 19:23:30 2018-01-01 19:23:40 2018-01-01 19:23:50 2018-01-01 19:24:00 2018-01-01 19:24:10 2018-01-01 19:24:20 2018-01-01 19:24:30 2018-01-01 19:24:40 2018-01-01 19:24:50 2018-01-01 19:25:00 2018-01-01 19:25:10 2018-01-01 19:25:20 2018-01-01 19:25:30 2018-01-01 19:25:40 2018-01-01 19:25:50 2018-01-01 19:26:00 2018-01-01 19:26:10 2018-01-01 19:26:20 2018-01-01 19:26:30 2018-01-01 19:26:40 2018-01-01 19:26:50 2018-01-01 19:27:00 2018-01-01 19:27:10 2018-01-01 19:27:20 2018-01-01 19:27:30 2018-01-01 19:27:40 2018-01-01 19:27:50 2018-01-01 19:28:00 2018-01-01 19:28:10 2018-01-01 19:28:20 2018-01-01 19:28:30 2018-01-01 19:28:40 2018-01-01 19:28:50 2018-01-01 19:29:00 2018-01-01 19:29:10 2018-01-01 19:29:20 2018-01-01 19:29:30 2018-01-01 19:29:40 2018-01-01 19:29:50 2018-01-01 19:30:00 2018-01-01 19:30:10 2018-01-01 19:30:20 2018-01-01 19:30:30 2018-01-01 19:30:40 2018-01-01 19:30:50 2018-01-01 19:31:00 ... 2018-01-02 05:22:10 2018-01-02 05:22:20 ... 2018-01-02 05:37:20 2018-01-02 05:37:30 2018-01-02 05:37:40 2018-01-02 05:37:50 2018-01-02 05:38:00 2018-01-02 05:38:10 2018-01-02 05:38:20 2018-01-02 05:38:30 2018-01-02 05:38:40 2018-01-02 05:38:50 2018-01-02 05:39:00
写入文本文件的方案:
package com.testEmp; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class PrintTime { public static void main(String[] args) throws Exception{ String currTime="2018-01-01 00:00:00"; File file = new File("d://datas.txt"); if(!file.exists()){ file.createNewFile(); } FileWriter fileWriter = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fileWriter); for(int i=0;i<3200000;i++) { System.out.println(currTime); currTime=timePastTenSecond(currTime); bw.write(currTime+"\n"); } bw.close(); System.out.println("File writing finished"); } public static String timePastTenSecond(String otime) { try { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dt=sdf.parse(otime); Calendar newTime = Calendar.getInstance(); newTime.setTime(dt); newTime.add(Calendar.SECOND,10);//日期加10秒 Date dt1=newTime.getTime(); String retval = sdf.format(dt1); return retval; } catch(Exception ex) { ex.printStackTrace(); return null; } } }
参考资料:
1.Java对日期Date类进行加减运算一二三 https://blog.csdn.net/hacker_lees/article/details/74351838
2.SimpleDateFormat的一些常用用法 https://www.cnblogs.com/qiangqiangqiang/p/7680752.html
3.Java IO 几种读写文件的方式 https://www.cnblogs.com/xiaoliu66007/p/9067883.html
--END-- 2019年10月5日08:05:16
[Java]给指定时间加上十秒的更多相关文章
- java 给指定时间加上天数or给当前日期加天数
给指定日期加上天数: /** * 指定日期加上天数后的日期 * @param num 为增加的天数 * @param newDate 创建时间 * @return * @throws ParseExc ...
- Java获取指定时间的毫秒值的方法
有以下两种方法获取指定时间的毫秒值: 1.Calendar类 先由getInstance获取Calendar对象,然后用clear方法将时间重置为(1970.1.1 00:00:00),接下来用set ...
- [Java]某日期时间加上若干分钟得到新的日期时间
使用Java自带类库实现日期时间增减还是比自己人工拆分编写要牢靠,代码也简洁多了. 下面代码实现了在原有日期时间上加上一些分钟得到新的日期时间的功能,稍加改造还可以实现逆向运算. 代码: packag ...
- java获取指定时间的年月日
作者:Night Silent链接:http://www.zhihu.com/question/35650798/answer/63983440来源:知乎著作权归作者所有,转载请联系作者获得授权.1. ...
- Java获取指定时间(转)
说明:从LocalDate的API上看,主要用于快速获取当前年月日,而DateFormatter也基本上伴随着使用.如果是操作Date对象的,主要是用于时间戳等,伴随着使用的是SimpleDateFo ...
- Java 得到指定时间加半个小时之后得时间
Calendar c = Calendar.getInstance(); c.setTime(cur); //设置时间 c.add(Calendar.MINUTE, 1); //日期分钟加1,Cale ...
- js获取指定时间的前几秒
最近项目上有一个需求是:根据一张图片的拍摄时间获取到这个时间前二后三的一个五秒钟的视频信息,通过查找相关资料写了一个方法拿来记录分享一下. //指定时间减2秒function reduceTwoS(d ...
- iOS规范化时间格式,object-C计算指定时间与当前的时间差
object-c计算指定时间与当前的时间差 头文件(.h): #import <Foundation/Foundation.h> @interface LuDate : NSDate +( ...
- java获取系统指定时间年月日
java获取系统指定时间年月日 private String setDateTime(String falg) { Calendar c = Calendar.getInstance(); c.set ...
随机推荐
- Vue Elementui中的Tag与页面其它元素相互交互
参考:https://www.jb51.net/article/147917.htm 思路 一.多选框勾选,出现对应的tag: 1.利用watch监听多选框绑定的值A(数组)的变化:2.根据A的变化, ...
- pip install win32api报错解决方法
在安装pywinauto模块,导入模块后,提示缺少:win32api 但是在使用pip install安装win32api后,居然报错 错误信息如下: Could not find a version ...
- Delphi 执行线程对象
- 12_Redis_服务器命令
一:Redis 服务器:Redis 服务器命令主要是用于管理 redis 服务
- 学习elasticsearch(一)linux环境搭建(1)
首先安装了Oracle Virtual Box 然后安装了最小版的CentOS.由于vbox自带的操作面板不太好用,于是用了xshell,XShell连接最小版的centOS时遇到的问题记录下. 1. ...
- C# 之 .net core -- EF code first连接Mysql数据库
一.在Models 新建两个数据库类 这个是数据库需要生成的类基础(塑造外观) public class User { [Key] public string ID { get; set; } [Ma ...
- 一段有关线搜索的从python到matlab的代码
在Udacity上很多关于机器学习的课程几乎都是基于python语言的,博主“ttang”的博文“重新发现梯度下降法——backtracking line search”里对回溯线搜索的算法实现也是用 ...
- CSS3 -- 边框圆角
文章后有彩蛋哦 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- Xftp6 和 Xshell 6 下载与安装使用
官网地址:https://www.netsarang.com/zh/all-downloads/ 然后进行安装吧,基本没有什么配置. 打开 xftp 连接 Linux,进行文件操作. 然后 名称:随意 ...
- httpClient请求响应延迟
客户端可以先向服务器端发送一个请求,如果服务器端返回的是状态码100,那么客户端就可以继续把请求体的数据发送给服务器端.这样在某些情况下可以减少网络开销. 再看看HttpClient里面对100-Co ...