2017-02-23 211 views
0
public class Date { 

    private int m; // month 
    private int d; // day 
    private int y; // year 

    private String month; // string month name ex. "January" 
    private int day; // int day 
    private int year;// year 

    private boolean equals; // make the dates equal 

    public Date(int m, int d, int y) { 
     this.m = m; 
     this.d = d; 
     this.y = y; 
    } 

    public Date(String month, int d, int y) { 
     this.month = month; 
     this.d = d; 
     this.y = y; 
    } 

    public boolean equals(Object other) { 
     if (!(other instanceof Date)) { 
      return false; 
     } 

     Date d2 = (Date) other; 

     if (y == d2.y) { 

      if (m == d2.m) { 

       if (d == d2.d) { 
        return true; 
       } else { 
        return false; 
       } 
      } else { 
       return false; 
      } 
     } else { 
      return false; 
     } 
    } 

    public String toString() { 

     if (m == 1) { 
      return "January " + d + ", " + y; 
     } else if (m == 2) { 
      return "February " + d + ", " + y; 
     } else if (m == 3) { 
      return "March " + d + ", " + y; 
     } else if (m == 4) { 
      return "April " + d + ", " + y; 
     } else if (m == 5) { 
      return "May " + d + ", " + y; 
     } else if (m == 6) { 
      return "June " + d + ", " + y; 
     } else if (m == 7) { 
      return "July " + d + ", " + y; 
     } else if (m == 8) { 
      return "August " + d + ", " + y; 
     } else if (m == 9) { 
      return "Septmember " + d + ", " + y; 
     } else if (m == 10) { 
      return "October " + d + ", " + y; 
     } else if (m == 11) { 
      return "November " + d + ", " + y; 
     } else if (m == 12) { 
      return "December " + d + ", " + y; 
     } else { 
      return month + " " + d + ", " + y; 
     } 

    } 

} 



import java.util.Random; 


public class DateDemo { 

    public static void test(Date d1, Date d2) { 
     if (d1.equals(d2)) { 
      System.out.println("\"" + d1 + "\" matches \"" + d2 + "\""); 
     } else { 
      System.out.println("\"" + d1 + "\" doesn't matche \"" + d2 + "\""); 
     } 
    } 

    public static void main(String[] args) { 
     test(new Date("January", 1, 1970), new Date(1, 1, 1970)); 
     test(new Date("December", 20, 1970), new Date(12, 20, 1971)); 
     test(new Date("July", 15, 1970), new Date(7, 14, 1970)); 
     test(new Date("July", 15, 1970), new Date(7, 15, 1970)); 
     test(new Date("November", 1, 1970), new Date(11, 1, 1970)); 
     test(new Date("April", 1, 1492), new Date(4, 1, 1492)); 
     test(new Date("March", 1, 1970), new Date(1, 1, 1970)); 

     Date d = new Date("January", 1, 1970); 
     if (d.equals(new String("Blah"))) { 
      System.out.println("Should not see me"); 
     } else { 
      System.out.println("Date can only possibly match another Date"); 
     } 
    } 
} 

對不起,這是我的第一篇文章,我需要幫助如何將月份名稱「1月」轉換爲整數「1」,以及如何使日期相等,當我運行它返回,他們不匹配。如何將字符串轉換爲int?

+0

封裝你幾個月的枚舉,那麼你可以利用序方法來獲取相關的整數。 – ferahgo

回答

0

提示

他們不是等於,因爲你有相同的值m = 0檢查,如果你讓這個在你的代碼:

System.out.println(y + "<--------->" + d2.y); 
if (y == d2.y) { 
    System.out.println(m + "<--------->" + d2.m); 
    if (m == d2.m) { 

你會得到這樣的結果,第一個日期:

1970<--------->1970 
0<--------->1 

試着解決之前,每件事情都會很好。

0

要一個月的字符串轉換爲整數,你可以使用switch語句是這樣的:

public int convert (String month) { 
    int number = 0; //just for the compiler 
    switch(month) { 
     case "January": int = 1 
         break; 
     case "February": int = 2 
         break; 
     ... 
     case "December": int = 12 
         break; 
    } 
    return number; 
} 
0

我會做月的枚舉。然後你可以使用枚舉序數+ 1來獲得你的整數。

public enum Months { 
    JANURARY, FEBUARY ... 
} 

然後,當你需要獲得相當於一個月

Months.JANUARY.ordinal() + 1; 
0

使用java.util.Calendar類的數量。它有一個可以使用的get方法。例如:Calendar.get(Calendar.MONTH)它爲您提供日曆實例所代表日期的值。您可以通過Calendar.getInstance()獲取日曆實例。這將爲您提供一個Calendar實例,它具有與您的系統時間相匹配的Locale和TimeZone。根據您的需要,您還可以選擇獲取自定義的區域設置和時區。

0

當您使用Date(String, int, int)構造函數創建Date對象時,您保留其int值(m變量)默認值(在這種情況下爲0)。這就是爲什麼equal方法不會給你所期望的結果。

要解決,你只需要基於這樣給定的字符串設定m變量在Date(String, int, int)問題:

public Date(String month, int d, int y) { 
    this.month = month; 
    this.m = monthFromStr(month); 
    this.d = d; 
    this.y = y; 
} 

private int monthFromStr(String str) { 
    if(str.equalsIgnoreCase("january")) 
     return 1; 
    // ... 
    throw new IllegalArgumentException("Unknown month!"); 
} 
0

你可以寫helper方法您的特定字符串轉換成具體的int類型。

import java.util.Random; 
class Date { 

    private int m; // month 
    private int d; // day 
    private int y; // year 

    private String month; // string month name ex. "January" 
    private int day; // int day 
    private int year;// year 

    private boolean equals; // make the dates equal 

    public Date(int m, int d, int y) { 
     this.m = m; 
     this.d = d; 
     this.y = y; 
    } 

    public Date(String month, int d, int y) { 
     this.month = month; 
     this.d = d; 
     this.y = y; 
    } 

    public int converter(String s){ 
    int i=0; 
    if (s.equals("January")){ 
     return 1; 
    } 
    if (s.equals("February")){ 
     return 2; 
    } 
    if (s.equals("March")){ 
     return 3; 
    } 
    if (s.equals("April")){ 
     return 4; 
    } 
    if (s.equals("May")){ 
     return 5; 
    } 
    if (s.equals("June")){ 
     return 6; 
    } 
    if (s.equals("July")){ 
     return 7; 
    } 
    if (s.equals("August")){ 
     return 8; 
    } 
    if (s.equals("Septmember")){ 
     return 9; 
    } 
    if (s.equals("October")){ 
     return 10; 
    } 
    if (s.equals("November")){ 
     return 11; 
    } 
    if (s.equals("December")){ 
     return 12; 
    } 
    return 0; 
    } 

    public boolean equals(Object other) { 
     if (!(other instanceof Date)) { 
      return false; 
     } 

     Date d2 = (Date) other; 
     if (month!=null){ 
      if (y == d2.y) { 
       if (this.converter(month) == d2.m) { 
        if (d == d2.d) { 
         return true; 
       } 
       else { 
        return false; 
       } 
      } else { 
       return false; 
      } 
     } else { 
      return false; 
     } 
     } 
     else 
     { 
      return false; 
     } 
    } 

    public String toString() { 

     if (m == 1) { 
      return "January " + d + ", " + y; 
     } else if (m == 2) { 
      return "February " + d + ", " + y; 
     } else if (m == 3) { 
      return "March " + d + ", " + y; 
     } else if (m == 4) { 
      return "April " + d + ", " + y; 
     } else if (m == 5) { 
      return "May " + d + ", " + y; 
     } else if (m == 6) { 
      return "June " + d + ", " + y; 
     } else if (m == 7) { 
      return "July " + d + ", " + y; 
     } else if (m == 8) { 
      return "August " + d + ", " + y; 
     } else if (m == 9) { 
      return "Septmember " + d + ", " + y; 
     } else if (m == 10) { 
      return "October " + d + ", " + y; 
     } else if (m == 11) { 
      return "November " + d + ", " + y; 
     } else if (m == 12) { 
      return "December " + d + ", " + y; 
     } else { 
      return month + " " + d + ", " + y; 
     } 

    } 

} 
public class DateDemo { 

    public static void test(Date d1, Date d2) { 
     if (d1.equals(d2)) { 
      System.out.println("\"" + d1 + "\" matches \"" + d2 + "\""); 
     } else { 
      System.out.println("\"" + d1 + "\" doesn't matche \"" + d2 + "\""); 
     } 
    } 

    public static void main(String[] args) { 
     test(new Date("January", 1, 1970), new Date(1, 1, 1970)); 
     test(new Date("December", 20, 1970), new Date(12, 20, 1971)); 
     test(new Date("July", 15, 1970), new Date(7, 14, 1970)); 
     test(new Date("July", 15, 1970), new Date(7, 15, 1970)); 
     test(new Date("November", 1, 1970), new Date(11, 1, 1970)); 
     test(new Date("April", 1, 1492), new Date(4, 1, 1492)); 
     test(new Date("March", 1, 1970), new Date(1, 1, 1970)); 

     Date d = new Date("January", 1, 1970); 
     if (d.equals(new String("Blah"))) { 
      System.out.println("Should not see me"); 
     } else { 
      System.out.println("Date can only possibly match another Date"); 
     } 
    } 
} 
2

要轉換一個月的名稱,它的整數值可以實際使用的Month#valueOf方法:

Month month = Month.valueOf("January".toUpperCase()); 
System.out.println(month.getValue() + " : " + month); 
// 1 : JANUARY