[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 ...
随机推荐
- base64转换成文件图片
最近搞小程序分享画布遇到的坑 canvas drawImage 传入的第一个参数是 imageResource 图片资源路径,这个参数通常由从相册选择图片 wx.chooseImage 或 wx.ge ...
- 4.Servlet(动态web资源)
Servlet (动态web资源) 开发一个动态web资源(即开发一个Java程序向浏览器输出数据) 需完成以下两个步骤 1.编写一个Java类,实现servelet接口 2.把开发好的Java类部署 ...
- Kinect 深度测量原理
和其他摄像机一样,近红外摄像机也有视场.Kinect摄像机的视野是有限的,如下图所示: 如图,红外摄像机的视场是金字塔形状的.离摄像机远的物体比近的物体拥有更大的视场横截面积.这意味着影像的高度和宽度 ...
- 使用Barrier分三步将大象放入冰箱
class Program { //构造大象和冰箱 private static ElephantsAndFridges elephantsAndFridges = new ElephantsAndF ...
- Python---virtualenv + Tensorflow + 安装jupyter notebook
一.ubuntu系统下安装完caffe后,安装 jupyter notebook. 在终端中执行,安装指令: sudo pip install jupyter 安装完成后运行 notebook : j ...
- java中静态代码块,非静态代码块,构造函数
关于静态代码块 静态代码块的写法: static { System.out.println("我是静态代码块"); } 静态代码块的特点: 1.执行优先级高于非静态的初始化块,它会 ...
- P1912 [NOI2009]诗人小G
P1912 [NOI2009]诗人小G 思路: 平行四边形不等式优化dp 因为f(j, i) = abs(sum[i]-sum[j]+i-j-1-l)^p 满足平行四边形不等式 j < i f( ...
- CodeForces - 1221E Game With String 分类讨论
首先分析A能获胜的情况 A能获胜 当且仅当A拿完后所有剩下的都<b 所以一旦存在一个大小为X的 且 b<=X<a 则必是后手赢 当X为 a<=x<2*b 的时候 无论A或 ...
- 用tensorflow实现SVM
环境配置 win10 Python 3.6 tensorflow1.15 scipy matplotlib (运行时可能会遇到module tkinter的问题) sklearn 一个基于Python ...
- spring boot2X代码混淆
为了防止代码很容易被反编译而造成泄露,所以打包时进行代码混淆 使用 proguard-maven-plugin插件 <build> <finalName>${artifactI ...