2013-02-13 31 views
-1

我試圖獲取客戶入口和出口日期,看看它們是否有效。 但每次運行程序時,隨機天數都會添加到月份中。例如在我進入03之後,該月可以是43。你能告訴我爲什麼嗎?公曆日曆自動添加到月份

public void makeNewReservation() throws ParseException { 

     // Enter informations 
     boolean error=true; 
     boolean dateOK=false; 
     String date_sortiee=""; 
     String date_entree=""; 
     Scanner in=new Scanner(System.in); 


     while(error){ 
      //take date of checkin and checkout and verify if they are OK 

      System.out.println("Single room selected ? dd/MM/yyyy"); 
      System.out.println("Date de Entree?"); 
      in=new Scanner(System.in); 

     // date_entree = in.next();  

      System.out.println("Date de Entree? dd/MM/yyyy"); 
      date_entree = in.next(); 
      String[] test = date_entree.split("/"); 
      int day = Integer.parseInt(test[0]); 
      int Month = Integer.parseInt(test[1]); 

      int year = Integer.parseInt(test[2]); 
      DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 
      GregorianCalendar checkin= new GregorianCalendar(); 
      checkin.set(year, Month, day); 

      System.out.println("Date de Sortie? dd/MM/yyyy"); 
      date_sortiee = in.next(); 
      String[] test1 = date_sortiee.split("/"); 
      int day1 = Integer.parseInt(test[0]); 
      int Month1 = Integer.parseInt(test[1]); 
      int year1 = Integer.parseInt(test[2]); 
      DateFormat format1 = new SimpleDateFormat("dd/MM/yyyy"); 
      GregorianCalendar checkout= new GregorianCalendar(); 
      //System.out.println(format.format(checkin)); 
      checkout.set(year1, Month1, day1); 


      checkdate(checkin,checkout); 


      dateOK= checkdate(checkin, checkout); 
      if (dateOK){ 
       error=false; 
      } 
      else{ 
       error=true; 
      } 
     } 


public boolean checkdate(GregorianCalendar in,GregorianCalendar out){ 
    DateFormat format = new SimpleDateFormat("dd/mm/yyyy"); 
    GregorianCalendar today = new GregorianCalendar(); 
    System.out.println(format.format(in.getTime())); 
    boolean test=false; 
    if(in.before(out)&&in.after(today)|| in.before(out)&& in.equals(today)){ 
     System.out.println("puck u"); 
     test= true; 

    } 
    return test; 
+3

你的示例代碼包含一個*很多*無意義的和分散的語句 - 包括兩個'DateFormat'變量,你永遠不會使用。那麼你的'checkDate'方法使用'mm'而不是'MM',但我們並不真正知道這是否是問題的一部分。請提供一個*簡短*但完整的程序來演示問題。 (順便說一下,我會避免自己解析這些值。) – 2013-02-13 18:48:45

+0

將'mm'更改爲'MM',並切換爲使用JodaTime。 – pcalcao 2013-02-13 18:48:45

+0

爲什麼你不使用你創建的'SimpleDateFormat'來做解析? 'checkin.parse(date_entree);'。 – 2013-02-13 18:49:35

回答

4

在了checkdate方法改變這種

DateFormat format = new SimpleDateFormat("dd/mm/yyyy"); 

DateFormat format = new SimpleDateFormat("dd/MM/yyyy"); 

正如你已經知道,MMmm格式爲分鐘格式,所以你得到適當的結果。

+0

日期入口:16/04/2013日期出口:19/05/2013 結果16/05/2013! – 2013-02-13 18:55:04

0
public boolean checkIfDateEntreeisok(Date date_entree, Date date_sortiee) throws ParseException{ 
    boolean dateOK=false; 

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 

    Date todaysDate = new Date(); 
    if((date_entree.after(todaysDate)) || (date_entree.equals(todaysDate))) { 
     System.out.println("first step"); 
     if ((date_entree.before(date_sortiee)) || (date_sortiee.equals(date_entree))) { 
      System.out.println("second step"); 

      dateOK=true; 
     } 

    } 

    else{ 
     System.out.println("les dates sont trop anciennes , veuillez les ressaisir"); 
    } 
    return dateOK; 
} 
0

看看在javadoc for Calendar.set(year, month, date)

你切實做好以下解析日期:

(每月不應該以大寫字母BTW開始)

...如果你閱讀javadoc,那麼你會發現在這個方法中月份是零(1月是0),所以對你來說事情會有點奇怪。例如:

  • 2013年1月1日 - 將是2013年2月1日
  • 31/12/2013 - 將是31日(只有12個月的一年)2014年1月
  • 31/1/2013 - 將2013年3月3日(僅28月天)

你可以這樣做:

checkin.set(year, month-1, day); 

或(我的偏好)這樣的:

checkin.setTime(format.parse(date_entree)); 

另外,你應該可以解決issue指出的@PermGenError