Java中怎么获取一个月的第几周的所有日期啊?比如说我要获取一月的第一周的所有天数的日期是多少要怎么写

2024-10-31 15:24:05
有4个网友回答
网友(1):

第几周是该月的第几周吗? 还是该年的第几周?
下面的程序包含了2种情况, 请自行选择.

package com.calender;

import java.util.Calendar;
import java.util.Locale;

/**
* 输出某周的日期.
* 演示Calendar的用法.
* Calendar的使用一般分2步,
* 1.确定某天, 调用set函数
* 2.可以取出该天的任意信息, 通过get函数.
* @author: zhuby
* @create date:2011-1-17
*/
public class WeekCalendar {
/**
* 周计数,为全年中的周计数
* @param year
* @param weekCount
* @return
*/
public int[] getWeekDateByWeekInYear(int year,int weekCount){

Calendar cal=Calendar.getInstance(Locale.CHINA);
//设置calendar的日期,此处可以确定某一天
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, weekCount);
//获取该天的信息(理论而言,通过改变get函数的参数,可以获取该天的任意信息)
int day=cal.get(Calendar.DAY_OF_YEAR);
int[] days=new int[7];
for(int i=0;i<7;i++){
days[i]=cal.get(Calendar.DAY_OF_MONTH);
// System.out.print((cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日 ");
day++;
cal.set(Calendar.DAY_OF_YEAR, day);
}
return days;
}
/**
* 周计数为月中的周计数
* @param year
* @param month
* @param weekCount
* @return
*/
public int[] getWeekDateByWeekInMonth(int year,int month,int weekCount){

Calendar cal=Calendar.getInstance(Locale.CHINA);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month-1);
cal.set(Calendar.WEEK_OF_MONTH, weekCount);
int day=cal.get(Calendar.DAY_OF_YEAR);
int[] days=new int[7];
for(int i=0;i<7;i++){
days[i]=cal.get(Calendar.DAY_OF_MONTH);
// System.out.print((cal.get(Calendar.MONTH)+1)+"月"+cal.get(Calendar.DAY_OF_MONTH)+"日 ");
day++;
cal.set(Calendar.DAY_OF_YEAR, day);
}
return days;
}

public static void main(String[] args) {
WeekCalendar wc=new WeekCalendar();

for(int i=0;i<7;i++){
System.out.print(wc.getWeekDateByWeekInYear(2011, 7)[i]+" ");
}
System.out.println();
for(int i=0;i<7;i++){
System.out.print(wc.getWeekDateByWeekInMonth(2011,4, 2)[i]+" ");
}

}
}

网友(2):

import java.util.Calendar;
import java.util.Locale;

public class WeekCalendar {

private int week;
private int year;
private int month;

public WeekCalendar(int year, int month, int week){
this.year = year;
this.month = month-1;//月份从0开始,所以减去1
this.week = week;
}

private static final int size = 7;

public static void main(String[] args){
WeekCalendar weekCal = new WeekCalendar(2011, 1, 2);
int[] weekDays = weekCal.getWeekDays();

for(int i = 0; i < weekDays.length; i++){
System.out.print(weekDays[i] + ", ");
}

}

public int[] getWeekDays(){

int[] weekDays = new int[size];

Calendar calendar = Calendar.getInstance(Locale.CHINA);

calendar.set(Calendar.WEEK_OF_MONTH, week);

for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++){
weekDays[i-1] = calendar.get(Calendar.DAY_OF_MONTH);
calendar.add(Calendar.DAY_OF_MONTH, 1);
}

return weekDays;
}
}

---------------
3, 4, 5, 6, 7, 8, 9,

网友(3):

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class Test1 {
public static void main(String[] args) {

Test1 t=new Test1();
List days=t.getWeekInMonth(2011,1,4);
for(int day:days){
System.out.print(day+" ");
}
}
private List getWeekInMonth(int year, int month, int index) {
List days=new ArrayList();
Calendar c=Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_WEEK_IN_MONTH,index);
days.add(c.get(Calendar.DATE));
for(int i=1;i<=6;i++){
c.add(Calendar.DATE, 1);
if(c.get(Calendar.MONTH)!=month-1)break;
days.add(c.get(Calendar.DATE));
}
return days;
}
}
基本正确吧,可能在判断第几星期上有些问题,你自己看应用场合修改下

网友(4):

一楼的calendar.set(Calendar.WEEK_OF_MONTH, week);前面少了calendar.set(Calendar.YEAR, year);calendar.set(Calendar.MONTH, month);,其他很好。二楼的答案更容易理解一些。我选择二楼的做法,学习了,谢谢。