# 日期函数的写法

denglala 2004-08-22 10:23:14

...全文
75 点赞 收藏 2

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;
}

Web 开发

8.0w+

Java Web 开发

2004-08-22 10:23