public void testSearchDate(){//日期查询
int year = 2013;//定义一个字段,接收输入的年份
Calendar calendar = new GregorianCalendar();//定义一个日历,变量作为年初
Calendar calendarEnd = new GregorianCalendar();//定义一个日历,变量作为年末
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);//设置年初的日期为1月1日
calendarEnd.set(Calendar.YEAR, year);
calendarEnd.set(Calendar.MONTH, 11);
calendarEnd.set(Calendar.DAY_OF_MONTH, 31);//设置年末的日期为12月31日
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
while(calendar.getTime().getTime()<=calendarEnd.getTime().getTime()){//用一整年的日期循环
if(calendar.get(Calendar.DAY_OF_WEEK)==5){//判断如果为星期四时,打印
System.out.println(sf.format(calendar.getTime()));
}
calendar.add(Calendar.DAY_OF_MONTH, 1);//日期+1
}
}
看能不能达到你想要的效果
public static void main(String[]args) {
int year = 2013;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,11);
int allWeek = calendar.get(Calendar.WEEK_OF_YEAR);
for(int i= 1; i <= allWeek ; i++ ){
System.out.println(fmtDate(getThursdayOfWeek(calendar.getTime(),i),""));
}
}
public static String fmtDate(Date date,String fmt){
if(date==null) return null;
SimpleDateFormat sdf = new SimpleDateFormat((fmt!=null && !"".equals(fmt)) ? fmt : "yyyy-MM-dd");
return sdf.format(date);
}
public static Date getThursdayOfWeek(Date year, int week) {
Calendar c = Calendar.getInstance();
c.setTime(year);
c.set(Calendar.WEEK_OF_YEAR, week);
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
return c.getTime();
}
感觉好像还有点问题 我再修改一下
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入年份:");
String input = sc.nextLine();
try {
int year = Integer.parseInt(input);
Calendar c = Calendar.getInstance();
c.clear();
//设置年份
c.set (Calendar.YEAR, year);
//设置星期四
c.set(Calendar.DAY_OF_WEEK, 5);
while(true){
if(c.get(Calendar.YEAR)!=year){
break;
}
System.out.println(c.getTime());
//增加1星期
c.add(Calendar.DAY_OF_WEEK, c.getMaximum(Calendar.DAY_OF_WEEK));
}
} catch (Exception e) {
System.out.println("输入有误!");
}
}
用Calendar API,整体思路是先找到第一个星期四,之后每隔7天取出日期
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CalendarTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintTuesday("2014");
}
public static void PrintTuesday(String year){ /*找到第一个星期四*/
String tempdate = year+"-01-01"; //先取第一天的值
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
try {
cal.setTime(formatter.parse(tempdate));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(cal.get(Calendar.DAY_OF_WEEK) == 5){ //如果第1天就是星期四,就直接调用全年星期四函数
PrintYearTuesday(cal);
}
else{
for(int i =0;i<7;i++){ //否则循环看看接下来7天内肯定有1天是星期四
cal.add(Calendar.DAY_OF_YEAR,1 );
if(cal.get(Calendar.DAY_OF_WEEK) == 5){
PrintYearTuesday(cal); //找到星期四后调用全年星期四函数
break; //找到后跳出,后面的循环无意义
}
}
}
}
public static void PrintYearTuesday(Calendar cal){ /*打印全年所有星期四*/
int tempyear = cal.get(Calendar.YEAR);
while(cal.get(Calendar.YEAR) == tempyear){
System.out.println(cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DAY_OF_MONTH));
cal.add(Calendar.DAY_OF_YEAR,7); //下个星期四肯定在7天后
}
}
}