• 主页

# 请教如何计算某一段日期内星期几是多少号？

nai598455803 2013-11-07 09:37:41

...全文
126 点赞 收藏 13

13 条回复

``````
select tt.tdate, to_char(tdate, 'day') as weekday
from (select (select to_date(t.start_date) from TEST_TABLE t where t.id = 2 ) + rownum - 1 as tdate
from all_objects
where rownum <= (select to_date(t.end_date) from TEST_TABLE t where t.id = 2 ) -
(select to_date(t.start_date) from TEST_TABLE t where t.id = 2 ) + 1)  tt where to_char(tt.tdate, 'day') in ('星期一','星期二') ``````

nai598455803 2013年11月07日

nai598455803 2013年11月07日
``````import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
* 以2012年12月18日为起点，
* 取出以后三个月中 周一，周三，周五的日期，
*
*/
public class Test
{
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();

cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DAY_OF_MONTH, 18);

Calendar c = (Calendar) cal.clone();

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

while(!c.before(cal))
{
int day = cal.get(Calendar.DAY_OF_WEEK) ;
if(day == Calendar.MONDAY || day == Calendar.WEDNESDAY || day == Calendar.FRIDAY)
System.out.println(df.format(cal.getTime()));
}
}
}``````

``````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]);
datenow = calendar.getTime();
nowTime = datenow.getTime();
}
}
}``````

nai598455803 2013年11月07日
start_date 是date 类型的， start_time 是int 类型的

chen2713756 2013年11月07日

nai598455803 2013年11月07日

SELECT DAYOFWEEK('xxx'); 刚发现这个还不错。

chen2713756 2013年11月07日

ohfiner 2013年11月07日

``````
select dayofweek(now());
``````

nai598455803 2013年11月07日

``````
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]);
}``````

Web 开发

5.2w+

34.1w+

Java Web 开发