请教如何计算某一段日期内星期几是多少号?
比如说,2013-11-7 到2013-11-30 这段日期内, 周一,周三,周日分别为10,11,13,17,18,20,24,25,27 日 , 如何才能通过程序取出来呢。
最好是通过sql 语句直接取出来,如果不能java 代码也行。
数据库中这么存的,start_date(2013-7-11) end_date(2013-11-30) days(1,3,7)
[解决办法]
网上找的。
public static void main(String[] args) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
String s = "2013-11-7";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
Date date = new Date();
try {
date = sdfInput.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0)
dayOfWeek = 0;
System.out.println(dayNames[dayOfWeek]);
}
select dayofweek(now());
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
String begin = "2013-11-01";
String end = "2013-11-10";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
Date dateb = new Date();
Date datee = new Date();
try {
dateb = sdfInput.parse(begin);
datee = sdfInput.parse(end);
} catch (ParseException e) {
e.printStackTrace();
}
long nowTime = dateb.getTime();
Date datenow = new Date();
datenow.setTime(nowTime);
Calendar calendar = Calendar.getInstance();
while(nowTime>=dateb.getTime()&&nowTime<=datee.getTime())
{
calendar.setTime(datenow);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0)
dayOfWeek = 0;
System.out.println(sdfInput.format(datenow));
System.out.println(dayNames[dayOfWeek]);
calendar.add(calendar.DATE,1);
datenow = calendar.getTime();
nowTime = datenow.getTime();
}
}
}