如何将String转换为int?

时间:2017-02-23 19:15:32

标签: java equals tostring

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");
        }
    }
}

对不起,这是我的第一篇文章,我需要帮助来解决如何将月份名称“january”转换为整数“1”以及如何使日期相等,当我运行它时它返回它们不匹配。

6 个答案:

答案 0 :(得分:2)

要将月份名称转换为它的整数值,您实际上可以使用Month#valueOf方法:

"

答案 1 :(得分: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

之前尝试解决这个问题,每件事情都会很好。

答案 2 :(得分:0)

要将月份的字符串转换为整数,您可以使用如下的开关语句:

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;
}

答案 3 :(得分:0)

使用java.util.Calendar课程。它有一个get方法,您可以使用它。例如:Calendar.get(Calendar.MONTH),它为您提供日历实例所代表日期的值。您可以Calendar.getInstance()获取日历实例。这将为您提供一个Calendar实例,该实例具有与您的系统时间匹配的Locale和TimeZone。您还可以根据需要选择自定义区域设置和TimeZone。

答案 4 :(得分:0)

使用Date构造函数创建Date(String, int, int)对象时,保留其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!");
}

答案 5 :(得分:0)

您可以编写辅助方法将特定字符串转换为特定的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");
        }
    }
}