根据某一固定日期获取 该日期所在年度的周,该日期所在周 周一和周日的 具体日期

        /**
*meteor
* 根据某一固定日期获取 该日期所在年度的周,该日期所在周 周一和周日的 具体日期
* @param date
* @return 
* map(date:传入的时间,week:第几周,monday:周一的日期[yyyy-MM-dd],sunday:周日的日期[yyyy-MM-dd])
*/
public static Map<String, String> getMapByDate(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Map<String, String> map = new HashMap<String, String>();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
map.put("date", sdf.format(cal.getTime()));
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
cal.setFirstDayOfWeek(Calendar.MONDAY);
map.put("week", cal.get(Calendar.WEEK_OF_YEAR) + "");
int day = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
map.put("monday", sdf.format(cal.getTime()));
cal.add(Calendar.DATE, 6);
map.put("sunday", sdf.format(cal.getTime()));
return map;
}

/**
* 该日期所在周 周一的 具体日期
* @param date
* @return
*/
public static Date getMondayOfWeekDate(Date date){
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
Map<String, String> map = getMapByDate(date);
Date reDate = null ;
try {
reDate = sdf.parse(map.get("monday"));
} catch (ParseException e) {
}
return reDate;
}
/**
* 该日期所在周 周日的 具体日期
* @param date
* @return
*/
public static Date getSundayOfWeekDate(Date date){
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
Map<String, String> map = getMapByDate(date);
Date reDate = null ;
try {
reDate = sdf.parse(map.get("sunday"));
} catch (ParseException e) {
}
return reDate;
}

/**
* 获取 该日期所在年度的周
* @param date
* @return
*/
public static String getWeekOfYearStr(Date date){
Map<String, String> map = getMapByDate(date);
String year = map.get("sunday").substring(0,4);
String week = map.get("week");
int strLen = week.length();
if (strLen < 2) {
while (strLen < 2) {
StringBuffer sb = new StringBuffer();
sb.append("0").append(week);
week = sb.toString();
strLen = week.length();
}
}
return year+week;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值