2017-02-23 262 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