java通过一个时间,得出本时间所在月份的所有时间

public static void main(String[] args) {
List<Date> list = getAllTheDateOftheMonth(new Date());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
for(Date date: list){
String format = formatter.format(date);
System.out.println(format);
}

}
private static List<Date> getAllTheDateOftheMonth(Date date) {
List<Date> list = new ArrayList<Date>();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
int month = cal.get(Calendar.MONTH);
while(cal.get(Calendar.MONTH) == month){
list.add(cal.getTime());
cal.add(Calendar.DATE, 1);
}
return list;
}
发布了31 篇原创文章 · 获赞 24 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/hengliang_/article/details/80109154