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]给指定时间加上十秒的更多相关文章

  1. java 给指定时间加上天数or给当前日期加天数

    给指定日期加上天数: /** * 指定日期加上天数后的日期 * @param num 为增加的天数 * @param newDate 创建时间 * @return * @throws ParseExc ...

  2. Java获取指定时间的毫秒值的方法

    有以下两种方法获取指定时间的毫秒值: 1.Calendar类 先由getInstance获取Calendar对象,然后用clear方法将时间重置为(1970.1.1 00:00:00),接下来用set ...

  3. [Java]某日期时间加上若干分钟得到新的日期时间

    使用Java自带类库实现日期时间增减还是比自己人工拆分编写要牢靠,代码也简洁多了. 下面代码实现了在原有日期时间上加上一些分钟得到新的日期时间的功能,稍加改造还可以实现逆向运算. 代码: packag ...

  4. java获取指定时间的年月日

    作者:Night Silent链接:http://www.zhihu.com/question/35650798/answer/63983440来源:知乎著作权归作者所有,转载请联系作者获得授权.1. ...

  5. Java获取指定时间(转)

    说明:从LocalDate的API上看,主要用于快速获取当前年月日,而DateFormatter也基本上伴随着使用.如果是操作Date对象的,主要是用于时间戳等,伴随着使用的是SimpleDateFo ...

  6. Java 得到指定时间加半个小时之后得时间

    Calendar c = Calendar.getInstance(); c.setTime(cur); //设置时间 c.add(Calendar.MINUTE, 1); //日期分钟加1,Cale ...

  7. js获取指定时间的前几秒

    最近项目上有一个需求是:根据一张图片的拍摄时间获取到这个时间前二后三的一个五秒钟的视频信息,通过查找相关资料写了一个方法拿来记录分享一下. //指定时间减2秒function reduceTwoS(d ...

  8. iOS规范化时间格式,object-C计算指定时间与当前的时间差

    object-c计算指定时间与当前的时间差 头文件(.h): #import <Foundation/Foundation.h> @interface LuDate : NSDate +( ...

  9. java获取系统指定时间年月日

    java获取系统指定时间年月日 private String setDateTime(String falg) { Calendar c = Calendar.getInstance(); c.set ...

随机推荐

  1. nhandled rejection Error: EPERM: operation not permitted, open 'C:\Program Files\nodejs\node_cache npm ERR! cb() never called!

    安装全局包时报错,之前已经遇到过,结果第二次又忘记解决方法,果然还是要记下来,好记性不如烂笔头哇 $ npm i electron -gUnhandled rejection Error: EPERM ...

  2. gulp 实现sass自动化 ,监听同步

    实现功能 监听scss文件 sass自动化 准备条件 1 .安装gulp npm init   ---->一直enter,会在当前目录下生成一个package.json文件,记录安装的依赖模块 ...

  3. JAVA语言程序设计课后习题----第八单元解析(仅供参考)

    1 本题主要考的是方法的克隆,与c++里面的拷贝有点相似,具体看书本p147 import java.util.Objects; public class Square implements Clon ...

  4. Scala高阶函数与泛型

    1. Scala中的函数 在Scala中,函数是“头等公民”,就和数字一样.可以在变量中存放函数,即:将函数作为变量的值(值函数). 2. scala中的匿名函数,即没有函数名称的函数,匿名函数常作为 ...

  5. Packet for query is too large (4,544,730 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.

    修改 my.ini 加上 max_allowed_packet =6710886467108864=64M默认大小4194304  也就是4M修改完成之后要重启mysql服务,如果通过命令行修改就不用 ...

  6. MySQL8.0.17下载与安装

    下载环境:Windows 10 下载地址: https://dev.mysql.com/downloads/mysql/ 1.解压压缩包,修改解压目录. 2.在E:\mysql-8.0.17-winx ...

  7. Linux 硬盘UUID相同处理方法

    OVF模板部署的linux虚拟机磁盘id是相同的,当同一个模板生成的虚拟机挂载虚拟机磁盘时就会遇到两个磁盘UUID相同的情况,导致系统启动后只能识别一个磁盘.这里介绍一下LVM分区的磁盘UUID相同的 ...

  8. idHTTP.Post

    procedure TForm4.Button2Click(Sender: TObject); var sResponse: string; EnvStr : TStringStream; begin ...

  9. 一图一知-TS之Interface接口

  10. Codeforces 920E-Connected Components? (set,补图,连通块)

    Connected Components? CodeForces - 920E You are given an undirected graph consisting of n vertices a ...