日期函数的写法

denglala 2004-08-22 10:23:14
请问函数:实现功能:传入“200408”(即2004年8月份)和“1”(找第几周)两个参数;得到一个数组(将2004年8月份的第一周的几天用数组返回),即返回20040801,20040802,20040803,20040804,20040805,20040806,20040807这个数组,谢谢大家。

...全文
107 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
mydeman 2004-08-22
  • 打赏
  • 举报
回复
曾经有人问过同样的问题,不过二楼的这位真的反应很快,已经把我上一次的回答,贴了出来,谢谢你了。不过下次希望你能注明出处!
MaoZhua 2004-08-22
  • 打赏
  • 举报
回复
public String[] getSingleWeek(String strDate, int intSingle){
String[] strWeekDays;

Calendar cal = Calendar.getInstance();
int intYear = Integer.parseInt(strDate.substring(0,4));
int intMonth = Integer.parseInt(strDate.substring(4)) - 1;
cal.set(intYear, intMonth, 1);

//
int intDay = cal.get(cal.DAY_OF_WEEK);
int intWeekNum = cal.get(cal.WEEK_OF_MONTH);
long longMillis = cal.getTimeInMillis();

//
if ( intWeekNum == intSingle ) {
strWeekDays = new String[8 - intDay];
for ( int i = 0; i < 8 - intDay; i ++) {
String strDateTemp = String.valueOf(cal.get(cal.DAY_OF_MONTH));
if (cal.get(cal.DAY_OF_MONTH) < 10) {
strDateTemp = "0" + strDateTemp;
}
strWeekDays[i] = strDate + strDateTemp;
longMillis += 24 * 60 * 60 * 1000;
cal.setTimeInMillis(longMillis);
}
}
else {
strWeekDays = new String[7];
longMillis += (intSingle - 2) * 7 * 24 * 60 * 60 * 1000 +
(7 - intDay) * 24 * 60 * 60 * 1000;
cal.setTimeInMillis(longMillis);
for ( int i = 0 ; i < 7 ; i ++) {
String strDateTemp = String.valueOf(cal.get(cal.DAY_OF_MONTH));
if (cal.get(cal.DAY_OF_MONTH) < 10) {
strDateTemp = "0" + strDateTemp;
}
strWeekDays[i] = strDate + strDateTemp;
longMillis += 24 *60 * 60 * 1000;
cal.setTimeInMillis(longMillis);
}
}

//
return strWeekDays;
}

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧