java对时间进行循环放到List中
现在想从8点开始循环,循环到次日的7点中,然后把数据放到list中,以下是需要存放到List中的数据集,不想做这样的数据,如何可以通过程序自动生成出来
time yl wd
8:00
9:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00
00:00
01:00
02:00
03:00
04:00
05:00
06:00
07:00
--------------------编程问答--------------------
--------------------编程问答-------------------- 相对来说用StringBuilder来实现字符串拼接是最好的。这里就不做例子了,你自己去试试吧 --------------------编程问答--------------------
import java.util.ArrayList;
import java.util.List;
public class TimeList {
private static List<String> time = new ArrayList<String>();
public static final int start =8;
public static List<String> setTimeList() {
String startTime = start+":00";
time.add(startTime);
for (int i = 0; i < 24; i++) {
if((start+i)>=24)
time.add(start+i-24+":00");
else
time.add(start+i+":00");
}
return time;
}
public static void main(String[] args) {
List<String> timelist =setTimeList();
for (String time : timelist) {
System.out.println(time);
}
}
}
public static void main(String[] args) { // TODO Auto-generated method
String str = "";
List<CalendarTestClass> list = null;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, 0);
for (int num = 0; num < 24; num++) {
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.add(Calendar.HOUR, num);
Date date = cal.getTime();
str = sdf.format(date);
System.out.println(str);
}
}
public class CalendarTestClass {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
帮我看一下我这么算出来的时间,怎么用List放到类CalendarTestClass 的name中,这样才是我想要的一个集合 --------------------编程问答-------------------- 你都做到这一步了,还不知道额
public static void main(String[] args) {
String str = "";
List<CalendarTestClass> list = new ArrayList<CalendarTestClass>();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, 0);
for (int num = 0; num < 24; num++) {
cal.set(Calendar.HOUR_OF_DAY, 8);
cal.add(Calendar.HOUR, num);
Date date = cal.getTime();
str = sdf.format(date);
list.add(getTime(str));
}
}
public CalendarTestClass getTime(String str){
CalendarTestClass calendar = new CalendarTestClass();
calendar.setName(str);
return calendar;
}
}
public class CalendarTestClass {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
补充:Java , Java EE