[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 ...
随机推荐
- SQL脚本优化
1.创建索引一.要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引 (1)在经常需要进行检索的字段上创建索引,比如要按照表字段username进行检索,那么就应 ...
- JavaSpring【六、AOP的API】
AOP API Spring1.2历史用法,现在仍然支持 现在xml配置和注解的用法是基于API的,只是比较简便
- JavaSpring【四、Bean管理注解实现】
前面讲的Bean相关配置全部是使用xml配置文件或实现接口来实现的,接下来将比较常用的用法,使用注解实现bean的注入和管理 内容包括 ClassPath扫描与组件管理 类的自动检测与注册bean c ...
- sql从n月到m月数据汇总,没有数据,当月显示0
做个备份 -- 按月份统计select date1, MONTHS, createtime, nvl(count2, 0)+count1 from ( SELECT TO_CHAR(ADD_MONTH ...
- HDU 4085 斯坦纳树+DP
https://cn.vjudge.net/problem/HDU-4085 给你n,m,k ,分别表示有n个点,m条边,每条边有一个权值,表示修复这条边需要的代价 从前k个点中任取一个使其和后k个点 ...
- metal sample code
https://developer.apple.com/metal/sample-code/ 又被我发现了个宝贝
- Force git to overwrite local files on pull 使用pull强制覆盖本地文件 转载自:http://snowdream.blog.51cto.com/3027865/1102441
How do I force an overwrite of local files on a git pull? I think this is the right way: $ git fetch ...
- cookie的使用和设置
cookie就是服务端通过浏览器端的存储机制,把一些会话相关数据存储在浏览器中.优点:分担服务端的压力,提高了效率,缺点:不安全 生成和请求原理 cookie的生命周期设定以后,哪怕是关闭浏览器,那么 ...
- Bootstrap-轮播图-No.5
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- <<代码大全>>阅读笔记之二 变量名的力量
1.变量命名的注意事项 1)可理解性 变量要望文知义,看到这个变量不用看其他的代码就知道这个变量表示什么意思 好的变量命:currentDate, heartRate 糟糕的变量名:newButton ...