- 此处的开始时间和结束时间,选了字符串形式,关于各种格式时间之间的转换,请继续浏览。
String timestamp = "Jan 8, 2020 3:02:38 PM";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMM d,yyyy K:m:s a", Locale.ENGLISH);
Date date = null;
String str = null;
Date d = new Date();
String s = null;
try {
date = sdf2.parse(timestamp);
str = sdf1.format(date);
s = sdf1.format(d);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(date);
System.out.println(str);
System.out.println(s);
getTest()实现了开始时间和结束时间之间的全部时间点
public static void getTest() {
String starttime = "20200101120800";
String endtime = "20200107140800";
//月
String smonth = starttime.substring(4,6);
//日
String sday = starttime.substring(6,8);
String emonth = endtime.substring(4,6);
String eday = endtime.substring(6,8);
//小时
String shour = starttime.substring(8, 10);
//分钟
String sminute = starttime.substring(10, 12);
String ehour = endtime.substring(8, 10);
String eminute = endtime.substring(10, 12);
String shStr = null;
String smStr = null;
ArrayList<String> list = new ArrayList();
//转换为整数
int sm = Integer.parseInt(smonth);
int em = Integer.parseInt(emonth);
int sd = Integer.parseInt(sday);
int ed = Integer.parseInt(eday);
int sh = Integer.parseInt(shour);
int eh = Integer.parseInt(ehour);
int sM = Integer.parseInt(sminute);
int eM = Integer.parseInt(eminute);
//两个时间不是同一天
if(!sday.equals(eday)){
if(ed > sd){
int differDay = ed - sd;
System.out.println("开始时间和结束时间的天数差值:" + differDay);
//获取第一天的所有时刻,从其时分开始
for (int i = sh; i < 24; i++) {
for (int j = sM; j < 60; j++) {
if (i < 10) {
shStr = "0" + i;
} else {
shStr = i + "";
}
if (j < 10) {
smStr = "0" + j;
} else {
smStr = j + "";
}
list.add(shStr + ":" + smStr);
}
}
System.out.println("----------------------------------");
int a = list.size();
System.out.println("第一天的时间数:" + a);
System.out.println("----------------------------------");
//所跨天数大于一天,遍历多个完整的一天
if(differDay > 1){
//需要遍历完整天数的次数
int num = differDay - 1;
while(num > 0){
//完整的一天的时刻
for (int i = 0; i < 24; i++) {
for (int j = 0; j < 60; j++) {
if (i < 10) {
shStr = "0" + i;
} else {
shStr = i + "";
}
if (j < 10) {
smStr = "0" + j;
} else {
smStr = j + "";
}
list.add(shStr + ":" + smStr);
}
}
num--;
}
}
System.out.println("----------------------------------");
int b = list.size();
System.out.println("中间完整天数的时间数:" + (b - a));
System.out.println("----------------------------------");
//最后一天,到所选时间的所有时刻
for (int i = 0; i <= eh; i++) {
for (int j = 0; j < 60; j++) {
if (i < 10) {
shStr = "0" + i;
} else {
shStr = i + "";
}
if (j < 10) {
smStr = "0" + j;
} else {
smStr = j + "";
}
list.add(shStr + ":" + smStr);
}
}
//计算出结束时,多算的分钟数
int t = 59 - eM;
//实际的时间数
int n = list.size() - t;
System.out.println("多算的分钟数:t=" + t);
for (int k = list.size() - 1; k >= n ; k--) {
list.remove(k);
}
System.out.println("----------------------------------");
int c = list.size();
System.out.println("最后一天的时间数:" + (c - b));
System.out.println("----------------------------------");
}
}else{
//开始时间和结束时间在同一天
if (eh >= sh) {
for (int i = sM; i < 60; i++) {
if (sh < 10) {
shStr = "0" + sh;
} else {
shStr = sh + "";
}
if (i < 10) {
smStr = "0" + i;
} else {
smStr = i + "";
}
list.add(shStr + ":" + smStr);
}
sh++;
while (eh > sh) {
System.out.println("sh= " + sh + ", eh= " + eh);
ArrayList<String> tempList = getAllTime(sh, eh);
list.addAll(tempList);
sh++;
}
if (sh < 24) {
for (int m = 0; m <= eM; m++) {
if (sh < 10) {
shStr = "0" + sh;
} else {
shStr = sh + "";
}
if (m < 10) {
smStr = "0" + m;
} else {
smStr = m + "";
}
list.add(shStr + ":" + smStr);
}
}
}
}
for (int k = 0; k < list.size(); k++) {
String str = list.get(k);
System.out.print(str + " ");
if (k != 0 && k % 60 == 0) {
System.out.println();
}
}
System.out.println();
System.out.println(list.size());
}