zoukankan      html  css  js  c++  java
  • [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+"
    ");
            }
            
            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

  • 相关阅读:
    07:常识性问题
    知识梳理
    Linux之防火墙iptables
    centos常用命令
    Centos7 内核升级及删除无用内核
    Java线程之Callable、Future
    Java线程之Timer
    Java线程之ThreadLocal
    Java线程之synchronized
    Java线程之wait()、notify()、notifyAll()
  • 原文地址:https://www.cnblogs.com/heyang78/p/11623894.html
Copyright © 2011-2022 走看看