public class TestDate {
public static void main(String[] args) throws Exception {
String beginDate = "2018-01-01";//开始时间
String endDate = "2018-02-11";//结束时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(beginDate));
Map<String, Object> map = new FastTreeMap();
for (long d = cal.getTimeInMillis(); d <= sdf.parse(endDate).getTime(); d = get_D_Plaus_1(cal)) {
map.put(sdf.format(d), "");
//list.add(sdf.format(d));
System.out.println(sdf.format(d));
}
System.out.println(map);
}
public static long get_D_Plaus_1(Calendar c) {
c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) + 1);
return c.getTimeInMillis();
}
}传入起止日期,返回这个时间段内的map
最新推荐文章于 2023-04-25 15:39:16 发布
本文提供了一个使用Java进行日期遍历的示例代码,通过该代码可以了解如何从一个指定的开始日期遍历到结束日期,并将这些日期存储到Map中。此示例展示了SimpleDateFormat、Calendar和FastTreeMap等类的应用。
1万+

被折叠的 条评论
为什么被折叠?



