如何在当前/某个月获取日历中的所有日期? 例如本月,如图片
结果是[" 07-31-2016"," 08-01-2016"," 08-02-2016" ..." 08-31-2016"," 09-01-2016"," 09-02-2016"," 09-03- 2016"]
任何想法?,提前谢谢。
答案 0 :(得分:4)
好吧,使用Calendar
及其常量,您可以轻松实现这一目标:
鉴于month
和year
获得该月的第一天并在星期一放置日历:
Calendar start = Calendar.getInstance();
start.set(MONTH, month - 1); // month is 0 based on calendar
start.set(YEAR, year);
start.set(DAY_OF_MONTH, 1);
start.getTime(); // to avoid problems getTime make set changes apply
start.set(DAY_OF_WEEK, SUNDAY);
if (start.get(MONTH) <= (month - 1)) // check if sunday is in same month!
start.add(DATE, -7);
给定month
和year
获取月份的最后一天并将日历移至星期日
Calendar end = Calendar.getInstance();
end.set(MONTH, month); // next month
end.set(YEAR, year);
end.set(DAY_OF_MONTH, 1);
end.getTime(); // to avoid problems getTime make set changes apply
end.set(DATE, -1);
end.set(DAY_OF_WEEK, SATURDAY);
if (end.get(MONTH) != month)
end.add(DATE, + 7);
测试一下:
public static void main(String[] args) {
int month = 8, year = 2016;
Calendar start = Calendar.getInstance();
start.set(MONTH, month - 1); // month is 0 based on calendar
start.set(YEAR, year);
start.set(DAY_OF_MONTH, 1);
start.getTime();
start.set(DAY_OF_WEEK, SUNDAY);
if (start.get(MONTH) <= (month - 1))
start.add(DATE, -7);
System.out.println(printCalendar(start));
Calendar end = Calendar.getInstance();
end.set(MONTH, month); // next month
end.set(YEAR, year);
end.set(DAY_OF_MONTH, 1);
end.getTime();
end.set(DATE, -1);
end.set(DAY_OF_WEEK, SATURDAY);
start.getTime();
if (end.get(MONTH) != month)
end.add(DATE, + 7);
System.out.println(printCalendar(end));
}
结合:
import static java.util.Calendar.*;
和
private final static SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
private static String printCalendar(Calendar c) {
return df.format(c.getTime());
}
<强>输出:强>
2016/07/31
2016/09/03
。通过强>
int month = 5, year = 2015;
<强>输出:强>
2015/04/26
2015/06/06
现在,只需在Calendar
循环中迭代开始+1
将Calendar.DATE
添加到while
(在示例中,我将分为几周以便更清楚):
int i = 1;
while (start.before(end)) {
System.out.print(printCalendar(start));
if (i % 7 == 0) { // last day of the week
System.out.println();
i = 1;
} else {
System.out.print(" - ");
i++;
}
start.add(DATE, 1);
}
<强>输出:强>
2015/04/26 - 2015/04/27 - 2015/04/28 - 2015/04/29 - 2015/04/30 - 2015/05/01 - 2015/05/02
2015/05/03 - 2015/05/04 - 2015/05/05 - 2015/05/06 - 2015/05/07 - 2015/05/08 - 2015/05/09
2015/05/10 - 2015/05/11 - 2015/05/12 - 2015/05/13 - 2015/05/14 - 2015/05/15 - 2015/05/16
2015/05/17 - 2015/05/18 - 2015/05/19 - 2015/05/20 - 2015/05/21 - 2015/05/22 - 2015/05/23
2015/05/24 - 2015/05/25 - 2015/05/26 - 2015/05/27 - 2015/05/28 - 2015/05/29 - 2015/05/30
2015/05/31 - 2015/06/01 - 2015/06/02 - 2015/06/03 - 2015/06/04 - 2015/06/05 - 2015/06/06
答案 1 :(得分:2)
您可以使用Java 8及更高版本中内置的漂亮java.time类。上述两种解决方案都可以工作,这是Java 8中的一种方法。可以稍微简化一下,将其拆分只是为了理解。
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
public class Clazz {
public static void main(String[] args) throws Exception {
LocalDate today = LocalDate.now();
LocalDate firstDayOfTheMonth = today.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDayOfTheMonth = today.with(TemporalAdjusters.lastDayOfMonth());
LocalDate squareCalendarMonthDayStart = firstDayOfTheMonth
.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
LocalDate squareCalendarMonthDayEnd = lastDayOfTheMonth
.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
List<LocalDate> totalDates = new ArrayList<>();
while (!squareCalendarMonthDayStart.isAfter(squareCalendarMonthDayEnd)) {
totalDates.add(squareCalendarMonthDayStart);
squareCalendarMonthDayStart = squareCalendarMonthDayStart.plusDays(1);
}
totalDates.forEach(System.out::println);
}
}
答案 2 :(得分:1)
在那个月的第一天之前获得星期一:
Calendar c = Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.MONDAY);
c.set(2016, 08, 01);
Calendar start = Calendar.getInstance();
start.setFirstDayOfWeek(Calendar.MONDAY);
start.setWeekDate(2016,c.getWeekYear(), Calendar.MONDAY);
在那个月的最后一天之后获得星期天:
c.set(2016,08,31);
Calendar end = Calendar.getInstance();
end.setFirstDayOfWeek(Calendar.MONDAY);
end.setWeekDate(2016, c.getWeekYear(), Calendar.SUNDAY);
然后打印开始和结束之间的所有日期
答案 3 :(得分:1)
像这样写一个常用的方法并使用-
fun getAllDateOfCurrentMonth(): List<LocalDate> {
val yearMonth= YearMonth.now()
val firstDayOfTheMonth = yearMonth.atDay(1)
val datesOfThisMonth = mutableListOf<LocalDate>()
for (daysNo in 0 until yearMonth.lengthOfMonth()){
datesOfThisMonth.add(firstDayOfTheMonth.plusDays(daysNo.toLong()))
}
return datesOfThisMonth
}