public class test{
public static void main(String[] args)throws ParseException{
String formatStart = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String startFormat = formatStart +" 09:00:00";
Date startParse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startFormat );
String formatEnd = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String endFormat = formatEnd +" 17:00:00";
Date endParse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTime(startParse );
List<Date> list = new ArrayList<>();
while(calendar.getTime().before(endParse )){
list.add(calendar.getTime());
calendar.add(Calendar.HOUR,+1)
}
list.add(endParse );
for(Date date : list){
System.out.println(date);
}
}
}
Tue Jul 19 09:00:00 CST 2022
Tue Jul 19 10:00:00 CST 2022
Tue Jul 19 11:00:00 CST 2022
Tue Jul 19 12:00:00 CST 2022
Tue Jul 19 13:00:00 CST 2022
Tue Jul 19 14:00:00 CST 2022
Tue Jul 19 15:00:00 CST 2022
Tue Jul 19 16:00:00 CST 2022
Tue Jul 19 17:00:00 CST 2022
#具体方法
abstract void add(int field, int amount)
boolean after(Object when)
boolean before(Object when)
int getActualMaximum(int field)
int getActualMinimum(int field)
int getFirstDayOfWeek()
abstract int getGreatestMinimum(int field)
static Calendar getInstance()
static Calendar getInstance(Locale aLocale)
static Calendar getInstance(TimeZone zone)
static Calendar getInstance(TimeZone zone, Locale aLocale)
abstract int getLeastMaximum(int field)
abstract int getMaximum(int field)
int getMinimalDaysInFirstWeek()
void setFirstDayOfWeek(int value)
void setMinimalDaysInFirstWeek(int value)
void setTime(Date date)
void setTimeInMillis(long millis)
String toString()