获得某年某月的第N周的日期

[b]本人工作至今3年有余,昨天花了2-3小时竟然没有把:通过指定某年某月的xx周的一周日期得到,无比伤心,今早通过自己码代码,也算是完成了这个需求,到网站找了很久也没有找到比较好的API,所以我贴出我自己的测试代码,如有更好的代码请分享一下,谢谢![/b]


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class WeekCalendar
{
/**
* 常见的日期格式
*/
public final static String DATEFORMAT003 = "yyyy-MM-dd";

// 本月第一周的开始日期
public final static int WEEK_1 = 1;

// 本月第二周的开始日期
public final static int WEEK_2 = 8;

// 本月第三周的开始日期
public final static int WEEK_3 = 15;

// 本月第四周的开始日期
public final static int WEEK_4 = 22;

// 本月第五周的开始日期(闰年二月有29天有五周,平年二月28天就只有四周了)
public final static int WEEK_5 = 29;

/**
* 根据指定的日期格式转换日期
*
* @param date 需要转换格式的日期
* @param formatStr 日期格式
* @return 转换之后的日期
*/
public static String dateConvetStringByFormat(Date date, String formatStr)
{
SimpleDateFormat myformat = new SimpleDateFormat(formatStr);
String after_date = null;
if (date != null)
{
after_date = myformat.format(date);
}
return after_date;
}

/**
* 获得指定月的最后一天
* @param year 指定的年
* @param month 指定的月
*
* @return Integer
*/
public static int getMonthLastDay(int year, int month)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, (month - 1));// 月份从0开始,得减去1
calendar.set(Calendar.DATE, 1);
// 得到这月的最后一天
calendar.roll(Calendar.DATE, -1);

return calendar.get(Calendar.DATE);
}

/**
* 获得某月的共几个周
*
* @param year 指定的年
* @param month 指定的月
*
* @return Integer[]
*/
public static int getMonthAllWeeks(int year, int month)
{
// 获得这一月的最后一天
int lastDay = getMonthLastDay(year, month);

// 最多五周
if (lastDay >= WEEK_5)
{
return 5;
}
return 4;
}

/**
* 获得xx月的第xx周的一周(7天)日期,拼凑成日期格式
*
* @param year 指定的年
* @param month 指定的月
* @param week 指定的周
*
* @return String[]
*/
public static String[] getMonthWeekDayCalendar(int year, int month, int week)
{
// 用来存储一周的日期
String[] dates = new String[7];

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, (month - 1));// 月份从0开始,得减去1

// 获得这月共有几周
int weekSum = getMonthAllWeeks(year, month);

// 判断输入的周数是否超过总周数
if (week <= weekSum)
{
// 没有超过总周数
int weekBeginDay = 1;

// 设置周的开始日期
switch (week)
{
case 2:
weekBeginDay = WEEK_2;
break;
case 3:
weekBeginDay = WEEK_3;
break;
case 4:
weekBeginDay = WEEK_4;
break;
case 5:
weekBeginDay = WEEK_5;
break;
}
// 获得这一月的最后一天
int lastDay = getMonthLastDay(year, month);

int index = 0;
// i<=lastDay 一般到了第五周就是29号了,不到7天了,必须要在下个月1号之前终止循环
for (int i = weekBeginDay; i <= lastDay; i++)
{
// 如果循环了7次,此次循环结束
if ((i - 7) >= weekBeginDay)
{
break;
}
calendar.set(Calendar.DATE, i);
// 格式化日期
dates[index] = dateConvetStringByFormat(calendar.getTime(),
DATEFORMAT003);
// System.out.println(dates[index]);
index++;
}
}

return dates;
}

public static void main(String[] args)
{
// 测试代码
getMonthWeekDayCalendar(2013, 6, 4);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值