Java 如何将字符串转换为int?

Java 如何将字符串转换为int?,java,equals,tostring,Java,Equals,Tostring,抱歉,这是我的第一篇帖子,我需要帮助,了解如何将月份名称“一月”转换为整数“1”,以及如何使日期相等,当我运行它时,返回的日期不匹配。要将月份字符串转换为整数,可以使用如下switch语句: public class Date { private int m; // month private int d; // day private int y; // year private String month; // string month name ex.

抱歉,这是我的第一篇帖子,我需要帮助,了解如何将月份名称“一月”转换为整数“1”,以及如何使日期相等,当我运行它时,返回的日期不匹配。

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

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");
        }
    }
}
Months.JANUARY.ordinal() + 1;

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

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");
        }
    }
}
Months.JANUARY.ordinal() + 1;

我会列举几个月。然后可以使用枚举序号+1来获取整数

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;
}
然后,当您需要获取与月份对应的数字时

public enum Months {
   JANURARY, FEBUARY ...
}

我会列举几个月。然后可以使用枚举序号+1来获取整数

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;
}
然后,当您需要获取与月份对应的数字时

public enum Months {
   JANURARY, FEBUARY ...
}

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

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

当您使用
Date(String,int,int)
构造函数创建
Date
对象时,您保留其int month值(
m
变量)默认值(在本例中为0)。这就是为什么
equal
方法不能给出预期的结果

要解决此问题,只需根据给定字符串设置
Date(String,int,int)
中的
m
变量,如下所示:

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");
        }
    }
}
Months.JANUARY.ordinal() + 1;

当您使用
Date(String,int,int)
构造函数创建
Date
对象时,将其int-month值(
m
变量)保留为默认值(在本例中为0)。这就是为什么
equal
方法不能给出预期的结果

要解决此问题,只需根据给定字符串设置
Date(String,int,int)
中的
m
变量,如下所示:

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");
        }
    }
}
Months.JANUARY.ordinal() + 1;

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

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

要将月份名称转换为整数值,实际上可以使用以下方法:


要将月份名称转换为整数值,实际上可以使用以下方法:


将月份封装在枚举中,然后可以利用ordinal方法获取关联的整数。将月份封装在枚举中,然后可以利用ordinal方法获取关联的整数。