需要帮忙解析字符串日期

huangapple go评论64阅读模式
英文:

Need help parsing string date

问题

到目前为止,我已经弄清楚了第一个月,但仍需要在日和年份方面寻求帮助。我在解析各个部分并将它们转换为整数方面遇到了困难。

int firstSlash = date.indexOf ("/");
month = Integer.parseInt (date.substring (0, firstSlash));

这是我目前的进展。

英文:

So far I have the first month figured out but I still need help with the day and year. I am having trouble parsing the individual pieces and converting them to integers.

int firstSlash = date.indexOf ("/");
month = Integer.parseInt (date.substring (0, firstSlash));

That is what I have so far.

答案1

得分: 2

简便方法

有一个名为split()的函数,它接受分隔符并返回一个字符串数组:

String[] words = date.split("/");
int month = Integer.parseInt(words[0]);
int day = Integer.parseInt(words[1]);
int year = Integer.parseInt(words[2]);

正确方法

当涉及从字符串解析日期时,首选的方法是使用Java的DateFormat API:

DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date theDate = format.parse(date);

Date对象更加强大,可以让您与日期和时间交互比仅使用整数更加流畅。

更多信息,请参见此处

英文:

Easy way

There is a function called split() that takes the delimiter and returns an array of strings:

String[] words = date.split("/");
int month = Integer.parseInt(words[0]);
int day = Integer.parseInt(words[1]);
int year = Integer.parseInt(words[2]);

Correct way

When it comes to parsing date from string, the preferred way is using Java DateFormat API:

DateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date theDate = format.parse(date);

Date object is much more powerful and allows you to interact with date and time much more fluently than bare ints

More info here!

答案2

得分: 0

如果您知道您的月份、年份和日期的位置,例如:25/10/2020,您可以直接使用分割函数。

如果您确定日期将以正确的格式出现:

String[] dateSplit = date.split("/");
int day = Integer.valueOf(dateSplit[0]);
int month = Integer.valueOf(dateSplit[1]);
int year = Integer.valueOf(dateSplit[2]);
System.out.println("年:" + year);
System.out.println("月:" + month);
System.out.println("日:" + day);

如果您不确定日期是否会以正确的格式出现:

String[] dateSplit = date.split("/");
if (dateSplit.length != 3) {
    throw new Exception("日期格式无效");
    // 或者执行其他操作,比如打印等...
}

try {
    int day = Integer.valueOf(dateSplit[0]);
    int month = Integer.valueOf(dateSplit[1]);
    int year = Integer.valueOf(dateSplit[2]);
    System.out.println("年:" + year);
    System.out.println("月:" + month);
    System.out.println("日:" + day);
} catch (NumberFormatException e) {
    throw new Exception("日期格式无效");
    // 或者执行其他操作,比如打印等...
}
英文:

If you know where are your month, year and day like : 25/10/2020 you can just use the split function

If you are SURE that the date will be in the right format :

String[] dateSplit = date.split("/");
int day = Integer.valueOf(dateSplit[0]);
int month = Integer.valueOf(dateSplit[1]);
int year = Integer.valueOf(dateSplit[2]);
System.out.println("year" + year);
System.out.println("month" + month);
System.out.println("day" + day);

If you are NOT SURE that the date will be in the right format :

String[] dateSplit = date.split("/");
if (dateSplit.length != 3) {
    throw new Exception("Date not in valid format");
    //or do something else like printing or whatever...
}

try {
    int day = Integer.valueOf(dateSplit[0]);
    int month = Integer.valueOf(dateSplit[1]);
    int year = Integer.valueOf(dateSplit[2]);
    System.out.println("year" + year);
    System.out.println("month" + month);
    System.out.println("day" + day);
} catch (NumberFormatException e) {
    throw new Exception("Date not in valid format");
    //or do something else like printing or whatever...
}

huangapple
  • 本文由 发表于 2020年10月9日 07:47:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64272088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定