思路:
**代码:以本月第一天顺序输出日期到当天日期为例
1、首先要得到本月第一天的日期
2、获取当月的天数day
3、循环输出
代码如下所示:
package com.chinasoft.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class praTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// 获取前月的第一天
Calendar cale = Calendar.getInstance();
cale = Calendar.getInstance();
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
String targetDay = format.format(cale.getTime());
System.out.println(printDay(targetDay));
}
public static Map<Integer,String> printDay(String dayTest) throws ParseException {
Map<Integer,String> map=new HashMap<>();
Calendar cale = Calendar.getInstance();
int day = cale.get(Calendar.DATE);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date d=null;
for (int i = 0; i < day; i++) {
d = new Date(f.parse(dayTest).getTime() + 24 * 3600 * 1000 * i);
map.put(i+1,f.format(d));
}
return map;
}
}
运行截图: