是这样的   最近做一航空订票系统  有一需求班期即一周:0,1,2,3,4,5,6    如果输入的是0,1,2,3, ,5,6则星期4没航班  不用往表里输入日期就是说再一段日期里,判断这些日期的星期是多少,如果星期等于输入的班期,则添加日期,如果不等于就不添加
比如:2009-07-26如果到2009-08-02,这些星期分别是星期日,星期一,星期二,星期三,星期四,星期五,星期六,星期日,如果我要求班期是0,1,2,3,,5,6则星期4不用输出日期,其他都输出。以下是我写的测试类,不知道哪有问题,不管输入是0,1,2,3,4,5,6还是别的都把日期给输出,就是没限制班期,希望大家帮帮忙,万分感激!
import java.util.*;
import java.text.*;
public class a {
    
public static void main(String args[]){

 try{
 String s = "0,1,2,3,,5,6"; 
 

String[] ss=new String[6]; 
ss=s.split(",");
String   startDate   =   "2009-07-26";   
String   endDate   =   "2009-08-02"; 
SimpleDateFormat   dateFormat=new   SimpleDateFormat("yyyy-MM-dd");   
 
    Date   a   = dateFormat.parse(startDate);     
    Date   b   = dateFormat.parse(endDate);  
    //System.out.print(a.getTime()-b.getTime());
long   periodMilli   =  b.getTime()   -   a.getTime()   ; 
int period=new Long(periodMilli/(86400000)).intValue();
 Calendar calendar=Calendar.getInstance();
         for(int i=0;0<=period;i++){
       
   
     
      calendar.setTime(a);
      int w=calendar.get(Calendar.DAY_OF_WEEK)-1;
       if(w<0){
       w=0;
       }
     
       String wl=String.valueOf(w);
      
 
         for(int j=0;j<ss.length;j++){

        
          if(wl.equals(ss[j])&&ss[j]!=null){
    //SimpleDateFormat   dateFormat2=new   SimpleDateFormat("yyyy-MM-dd"); 
      // String xx=dateFormat.format(calendar.getTime());
      
    
    
      //calendar.setTime(a);
             calendar.set(Calendar.DAY_OF_YEAR,(calendar.get(Calendar.DAY_OF_YEAR)+ period));
             String yy=dateFormat.format(calendar.getTime());
             System.out.println(yy);                  
       }
     // System.out.println(ss[j]);
        
      }
  
       period--;
      
       
        }  
         
         
   
   }catch(ParseException xx){}
}
}现在输出值是
2009-08-02
2009-08-01
2009-07-31
2009-07-30
2009-07-29
2009-07-28
2009-07-27
2009-07-26     先谢谢大家了。

解决方案 »

  1.   

    看看行不try
    {
    String s = "0,1,2,3,,5,6";
    String startDate = "2009-07-26";
    String endDate = "2009-08-02";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    Date a = dateFormat.parse(startDate);
    Date b = dateFormat.parse(endDate);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(a);
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(b);
    String temp = "";
    while (calendar.before(calendar1) || calendar.equals(calendar1))
    {
    temp = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK) - 1);
    if (s.indexOf(temp) > -1)
    {
    System.out.println(dateFormat.format(calendar.getTime()));
    }
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    }
    catch (ParseException xx)
    {
    }
      

  2.   

    package com.datacomo.test;import java.util.*;
    import java.text.*;public class a { public static void main(String args[]) { try {
    String s = "0,1,2,3,,5,6"; String[] ss = new String[6];
    ss = s.split(",");
    String startDate = "2009-07-26";
    String endDate = "2009-08-02";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date a = dateFormat.parse(startDate);
    Date b = dateFormat.parse(endDate); // System.out.print(a.getTime()-b.getTime());
    long periodMilli = b.getTime() - a.getTime();
    int period = new Long(periodMilli / (86400000)).intValue();
    Calendar calendar = Calendar.getInstance(); for (int i = 0; 0 <= period; i++) { calendar.setTime(a);
    int w = calendar.get(Calendar.DAY_OF_WEEK) + i;
    if (w > 6) {
    w = 0;
    } String wl = String.valueOf(w); for (int j = 0; j < ss.length; j++) { if (wl.equals(ss[j]) && ss[j] != null
    && !ss[j].trim().equals("")) {
    // SimpleDateFormat dateFormat2=new
    // SimpleDateFormat("yyyy-MM-dd");
    // String xx=dateFormat.format(calendar.getTime()); // calendar.setTime(a);
    calendar.set(Calendar.DAY_OF_YEAR, (calendar
    .get(Calendar.DAY_OF_YEAR) + period));
    String yy = dateFormat.format(calendar.getTime());
    System.out.println(yy);
    }
    // System.out.println(ss[j]); } period--; } } catch (ParseException xx) {
    }
    }
    }
    逻辑稍微有些问题,小问题,lz自己应该也能弄的
      

  3.   

    写了个类,楼主看看,是否合乎要求。
    public class FlightsPeriod {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private Set<Integer> periods = new TreeSet<Integer>();
    private Date start ;
    private Date end;

    public FlightsPeriod(String startDay,String endDay) throws ParseException{
    start = sdf.parse(startDay);
    end = sdf.parse(endDay);
    }

    public FlightsPeriod appendPeriod(int period){
    periods.add(period);
    return this;
    } public void printPeriodDays(){
    printPeriodDays(System.out);
    }
    public void printPeriodDays(PrintStream out){
    Calendar cal = Calendar.getInstance();
    cal.setTime(start);
    Calendar endCal = Calendar.getInstance();
    endCal.setTime(end);endCal.add(Calendar.SECOND, 1);
    while(cal.before(endCal)){
    int weekDay = cal.get(Calendar.DAY_OF_WEEK);
    if(periods.contains(weekDay))out.println(sdf.format(cal.getTime()));
    cal.add(Calendar.DAY_OF_YEAR, 1);
    }
    }

    public static void main(String[] args) throws ParseException {
    FlightsPeriod fp = new FlightsPeriod("2009-07-26","2009-08-02");
    fp.appendPeriod(Calendar.SUNDAY).appendPeriod(Calendar.MONDAY).appendPeriod(Calendar.TUESDAY);
    fp.appendPeriod(Calendar.THURSDAY).appendPeriod(Calendar.FRIDAY).appendPeriod(Calendar.SATURDAY);
    fp.printPeriodDays();
    }
    }
      

  4.   

    int w = calendar.get(Calendar.DAY_OF_WEEK) + i;
    if (w > 6) { 
    w = 0; 

    每次+1,到>6的时候返回0,这样才能循环获取星期几,你写的时候值是不变的,w返回的都是0
      

  5.   

    楼主的代码太乱了import java.util.*; 
    import java.text.*;public class ab { 
    public static void main(String args[]){ 
    String s = "0,1,2,3,,5,6"; 
    List<String> ss=Arrays.asList(s.split(","));
    System.out.println(ss); 
    String  startDate  =  "2009-07-26";  
    String  endDate  =  "2009-08-02"; 
    SimpleDateFormat  sdf=new  SimpleDateFormat("yyyy-MM-dd");
    Date a=new Date(),b=new Date();
    try{
    a  = sdf.parse(startDate);    
    b  = sdf.parse(endDate);  
    }catch(Exception e){
    e.printStackTrace();
    }
    Calendar ac=Calendar.getInstance(); 
    Calendar bc=Calendar.getInstance(); 
    ac.setTime(a);
    bc.setTime(b);
    for(Calendar i=ac;i.compareTo(bc)<=0;i.set(Calendar.DAY_OF_MONTH ,i.get(Calendar.DAY_OF_MONTH)+1)){ 
    int week=i.get(Calendar.DAY_OF_WEEK);
    if(ss.contains(String.valueOf(week-1))){
    System.out.println(sdf.format(i.getTime()));
    }
    }
    }
    }结果:
    F:\java>java ab
    [0, 1, 2, 3, , 5, 6]
    2009-07-26
    2009-07-27
    2009-07-28
    2009-07-29
    2009-07-31
    2009-08-01
    2009-08-02
      

  6.   

    为什么不用1,2,3,4,5,6,7来表示星期日,星期一....星期六?
    这样就和Canlendar类一致了。
      

  7.   

    好像不是很复杂
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;public class MyTest {
    public static void main(String[] args){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    Calendar calendar=Calendar.getInstance();
    String sDateFilter="0,1,2,3, ,5,6";

    try{
    Date dateS=sdf.parse("2009-07-25");
    Date dateE=sdf.parse("2009-08-02");
    Date date=dateS;
    String  index;

    calendar.setTime(dateS);

    while(date.compareTo(dateE)<=0){
    index=(calendar.get(calendar.DAY_OF_WEEK)-1)+"";

    if(sDateFilter.indexOf(index)!=-1){
    System.out.println(sdf.format(date));
    }

    calendar.add(calendar.DATE,1);
    date=calendar.getTime();
    }

    }catch(Exception ex){
    ex.printStackTrace();
    }

    }
    }
      

  8.   


    package test;import java.util.*;
    import java.text.*;
    public class Csdna {
    public static void main(String args[]){
    try{
        String s = "0,1,2,3,,5,6";
        String[] arr = s.split(",");
    String  startDate  =  "2009-07-26"; 
    String  endDate  =  "2009-08-02";
    SimpleDateFormat format =new  SimpleDateFormat("yyyy-MM-dd"); 

    Date  start  = format.parse(startDate);   
    Date  end  = format.parse(endDate); 
    Calendar cal = Calendar.getInstance();
    cal.setTime(start);
    int count = 0;
    String week = null;
    for(Date tempDate = start; tempDate.compareTo(end)<=0 && count<arr.length;){
    week = arr[count];
    if(!"".equals(week) && Integer.parseInt(week)+1 == cal.get(Calendar.DAY_OF_WEEK)){
    System.out.println(format.format(tempDate));
    }

    cal.add(Calendar.DAY_OF_MONTH, 1);
    tempDate = cal.getTime();
    count ++;
    }

    }catch(ParseException xx){}
    }
    }
    看看这个有没有用处
      

  9.   

    我晕
    我怎么没有看见API中的add方法.
      

  10.   

    我去吃个饭  让大家再说说就结贴啦总之谢谢大家啦   谢谢CSDN