java - 如何将 String 转换为 int?

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

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

最佳答案

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

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

关于java - 如何将 String 转换为 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42424267/

相关文章:

Java:要在充满自定义对象的 ArrayList 中使用包含,我应该覆盖 equals 还是实现 Comparable/Comparator?

java - 无法使用 jpa 更新对象

java - 如何打印我的 Java 对象而不得到 "SomeType@2f92e0f4"?

c# - 在 C# 中,在对字符串或字符使用 ToString() 时,在什么情况下我需要提供 CultureInfo IFormatProvider?

JavaFX - 如何为标签中的单词应用不同的颜色

java - 从 Java 运行 linux 脚本

java - 如果两个不同的对象具有相同的哈希码会怎样?

java - Travis Java 构建使用不同的 SDK 进行编译和测试

java - 用 java 托管 RESTful Web 服务

kotlin - 为什么将单个char和 “single char String”转换为long(.toLong())时不相等