2017-08-02 131 views
-1

我輸入的字符串轉換爲日期格式。當我嘗試給出兩個日期作爲輸入Date of BirthDate of Joining,我沒有得到任何滿意的結果。如果輸入無效,也可以幫助我重新提示。提前致謝。日期有兩個日期

public class EmployeeInfo { 
int id; 
static String name, DoBS, DoJS; 
Date DoB, DoJ; 

public void checkDate(String dt) throws ParseException { 

    SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy"); 
    SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy"); 
    SimpleDateFormat sdf3 = new SimpleDateFormat("dd MMMM yyyy"); 

    Date date = null; 

    try { 
     date = sdf1.parse(dt); 
    } catch (ParseException e) { 
     try { 
      date = sdf2.parse(dt); 
     } catch (ParseException e1) { 
      try { 
       date = sdf3.parse(dt); 
      } catch (ParseException e2) { 
       String invalid="Invalid,Retry"; 
       System.out.println(invalid); 
       // TODO: Whatever to do when it doesn't find a date 
      } 
     } 
    } 

     setDateOfBirth(date); 
     setDateOfJoining(date); 
} 


void setDateOfBirth(Date DoB) { 
    this.DoB = DoB; 
} 

void setDateOfJoining(Date DoJ) { 
    this.DoJ = DoJ; 
} 



void print() { 
    System.out.println("User ID: " + id); 
    System.out.println("Name: " + name); 
    System.out.println("Date Of Birth: " + DoB); 
    System.out.println("Date of Joining: " + DoJ); 
} 

public static void main(String[] args) throws ParseException { 
    Scanner scanner = new Scanner(System.in); 
    System.out.println("Enter the name: "); 
    name = scanner.nextLine(); 

    System.out.println("Enter the Date Of Birth: "); 
    DoBS = scanner.nextLine(); 

    System.out.println("Enter the Date of Joining: "); 
    DoJS = scanner.nextLine(); 

    EmployeeInfo e = new EmployeeInfo(); 

    e.checkDate(DoBS); 
    e.checkDate(DoJS); 
    e.print(); 

} 
} 
+0

你不需要內爲''try' try' –

+0

輸入的姓名,出生日期和加入的日期。我想把這些東西打印回來。日期驗證的東西.. –

+1

代碼將是這樣多少「冷」,如果你有使用一些方法「的TryParse」或類似的東西 – 2017-08-02 11:31:02

回答

0

於是我就和它固定爲你,它有點砍死在一起,但工作得很好 首先,你應該有EmployeeInfo作爲單獨的類 EmployeeInfo.java

public class EmployeeInfo { 
    int id; 
    static String name; 
    Date DoB, DoJ; 

    public void setName(final String name) { 
     this.name = name; 
    } 

    public void setDateOfBirth(final Date DoB) { 
     this.DoB = DoB; 
    } 

    public void setDateOfJoining(final Date DoJ) { 
     this.DoJ = DoJ; 
    } 

    void print() { 
     System.out.println("User ID: " + id); 
     System.out.println("Name: " + name); 
     System.out.println("Date Of Birth: " + DoB); 
     System.out.println("Date of Joining: " + DoJ); 
    } 
} 

然後你應該有運行,並擁有所有的邏輯 Main.java

public class Main {  
    public static Date checkDate(final String dt) throws ParseException { 

     final SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy"); 
     final SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy"); 
     final SimpleDateFormat sdf3 = new SimpleDateFormat("dd MM yyyy"); 

     Date date = null; 

     try { 
      date = sdf1.parse(dt); 
     } catch (final ParseException e) { 
      try { 
       date = sdf2.parse(dt); 
      } catch (final ParseException e1) { 
       try { 
        date = sdf3.parse(dt); 
       } catch (final ParseException e2) { 
        final String invalid = "Invalid,Retry"; 
        System.out.println(invalid); 
       } 
      } 
     } 
     return date; 
    } 

    public static void main(final String[] args) throws ParseException { 
     final Scanner scanner = new Scanner(System.in); 
     final EmployeeInfo e = new EmployeeInfo(); 
     System.out.println("Enter the name: "); 
     e.setName(scanner.nextLine()); 
     Date d = null; 
     while (d == null) { 
      System.out.println("Enter the Date Of Birth: "); 
      d = checkDate(scanner.nextLine()); 
     } 
     e.setDateOfBirth(d); 
     d = null; 
     while (d == null) { 
      System.out.println("Enter the Date of Joining: "); 
      d = checkDate(scanner.nextLine()); 
     } 
     e.setDateOfJoining(d); 

     e.print(); 

    } 
} 

希望這個主類讓你瞭解它是如何完成的。我懇求你比較兩個源代碼,你的和我的,並試圖理解什麼已經改變,並試圖找出原因(在你自己)。

+0

當我輸入一個有效的日期,異常發生..爲什麼如此? –

+0

剛注意到,日期格式沒有任何意義。更新代碼 – PanBrambor

+0

我認爲這不是錯誤..在'd = checkDate(scanner.nextLine());'在主要方法中的東西。 –