10°

测试生成三个月内所有时间包括时分秒

注:此代码为测试使用,就简单写了下测试数据 方便复制粘贴
Integer year = 2020;
Integer mon = 1;
Integer data = 1;
Integer hour = 0;
Integer min = 0;
Integer senc = 0;
for (int i = 0; i < 100000000; i++) {
    if (senc>=59){
        min++;
        senc = 0;
}
if (min&gt;=59){
    hour++;
    min=0;
    senc = 0;
}
if (hour&gt;=23){
    data++;
    hour=0;
    min=0;
    senc = 0;
}
if (data&gt;=30){
    mon++;
    data=0;
    hour=0;
    min=0;
    senc = 0;
}
if (mon&gt;=4){
    data=0;
    hour=0;
    min=0;
    senc = 0;
    break;
}
senc++;
String year1 = String.format("%02d", year);//2是最少两位补   不是两位自动补0
String mon1  = String.format("%02d", mon);
String data1 = String.format("%02d", data);
String hour1 = String.format("%02d", hour);
String min1  = String.format("%02d", min);
String senc1 = String.format("%02d", senc);
String dataInfo= year1+"/"+mon1+"/"+data1+" "+hour1+":"+min1+":"+senc1;
System.out.println(dataInfo);

}

打印如下:

2020/01/01 01:18:54 2020/01/01 01:18:55 2020/01/01 01:18:56 2020/01/01 01:18:57 2020/01/01 01:18:58 2020/01/01 01:18:59 2020/01/01 01:19:01 2020/01/01 01:19:02 2020/01/01 01:19:03 2020/01/01 01:19:04

 

本文由【久伴诗与远方】发布于开源中国,原文链接:https://my.oschina.net/u/3774949/blog/3136469

全部评论: 0

    我有话说: