我想查找給定月份中的所有星期六和星期天。我該怎麼做?如何在給定的月份中查找週六和週日?
4
A
回答
8
最簡單的方法是隻遍歷一個月中的所有日子,並檢查每個星期中的每一天。例如:
// This takes a 1-based month, e.g. January=1. If you want to use a 0-based
// month, remove the "- 1" later on.
public int countWeekendDays(int year, int month) {
Calendar calendar = Calendar.getInstance();
// Note that month is 0-based in calendar, bizarrely.
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
int count = 0;
for (int day = 1; day <= daysInMonth; day++) {
calendar.set(year, month - 1, day);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.SUNDAY || dayOfweek == Calendar.SATURDAY) {
count++;
// Or do whatever you need to with the result.
}
}
return count;
}
我絕對相信有這樣做的更有效的方法 - 但是這就是我下手,優化當我發現它的速度太慢。
請注意,如果你能使用Joda Time,將讓您的生活輕鬆了許多......
1
試試這個:在以前的答案
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Sundays {
public static void main(String[] args) {
int year = 2012;
// put the month you want
int month = Calendar.JANUARY;
Calendar cal = new GregorianCalendar(year, month, 1);
do {
int day = cal.get(Calendar.DAY_OF_WEEK);
if (day == Calendar.SATURDAY || day == Calendar.SUNDAY) {
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
}
cal.add(Calendar.DAY_OF_YEAR, 1);
} while (cal.get(Calendar.MONTH) == month);
}
}
+0
感謝您的回覆 – Venkat 2012-03-29 04:50:15
1
0
請經過這個方法,我面對很多最後我創造了我自己的。
public static int getDayDiff(String dateFrom, String dateTo){ // DD/MM/YYYY
Timestamp tDtF = getTimestampDDmmYYYY(dateFrom);//returning timestamp from/DD/MM/YYYY
Timestamp tDtT = getTimestampDDmmYYYY(dateTo);
Calendar dtF = new GregorianCalendar();
Calendar dtT = new GregorianCalendar();
dtF.setTimeInMillis(tDtF.getTime());
dtT.setTimeInMillis(tDtT.getTime());
int count = 0;
while(dtF.before(dtT)){
count++;
dtF.add(Calendar.DAY_OF_YEAR, 1);
}
return count;
}
public static int countDateSatOrSun(String dateFrom, String dateTo){//DD/MM/YYYY
Timestamp tDtF = getTimestampDDmmYYYY(dateFrom);//returning timestamp from/DD/MM/YYYY
Timestamp tDtT = getTimestampDDmmYYYY(dateTo);
Calendar dtF = new GregorianCalendar();
Calendar dtT = new GregorianCalendar();
dtF.setTimeInMillis(tDtF.getTime());
dtT.setTimeInMillis(tDtT.getTime());
int count = 0;
while(dtF.before(dtT)){
if((dtF.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY| dtF.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY))
count++;
dtF.add(Calendar.DAY_OF_YEAR, 1);
}
return count;
}
它會給你確切的結果。
相關問題
- 1. NSCalendar查找本月的週六和週日
- 2. 查找日期範圍一週的特定月份和年份
- 3. 計算給定月份和年份的週日數
- 4. 如何獲取給定月份中每週的日期間隔?
- 5. 給定年,月,日和月的週數,我如何找到日期?
- 6. 查找特定月份和年份的一週號碼
- 7. java在月份的不同週中查找日期
- 8. 從日,周和年的查找月份Python
- 9. 如何總週日期爲給定月份獲得SQL
- 10. 計算給定月份的週數
- 11. 在SQL Server中獲取每週給定的一週數,月份數和年份的日期範圍
- 12. 查找給定週數的日期
- 13. 獲取月份的第一個星期六和上週月
- 14. 月份週一至週日的日曆顯示日期
- 15. 集團由週日期和月份
- 16. 如何統計給定月份中的週末數(碳對象)?
- 17. 獲取最近的週日和週六在本週與PHP
- 18. 計算天,不算週六和週日
- 19. MDX獲得上週日和週六
- 20. 查找給定月份和特定年份的具體日期
- 21. 在給定的幾周(幾個月)之後在ASP.NET中查找日期
- 22. 爲了讓週一至週六當月
- 23. 週末不是週六/週日
- 24. 列出週一到週五的月份,從週一到週日碳
- 25. 如何獲取給定週數和年份的首末日期?
- 26. java - 獲取給定月份每週的開始日期和結束日期
- 27. Informix查詢EXCLUDE本週的週五和週六以及週四和週日的下午4點到8點
- 28. 如何在psql中獲取當前月份週日的數量?
- 29. 週數到月份
- 30. 從Datetime.Now查找當前周,以及如何查找當前週日期之間的給定日期?
感謝您的回覆 – Venkat 2012-03-29 04:35:16
感謝您的代碼。 。 – 2017-11-28 07:36:32