【问题标题】:Parsing date with Joda API使用 Joda API 解析日期
【发布时间】:2013-12-04 14:31:32
【问题描述】:

我有一个包含日期“1985 年 12 月 25 日”的字符串。如何使用 Joda API 解析它?

DateTime dt = new DateTime("25 December 1985");
System.out.println(dt.getDayOfWeek());

我需要一周中的某一天。

【问题讨论】:

    标签: java parsing jodatime


    【解决方案1】:

    试试这个:

    DateTimeFormatter dtf = DateTimeFormat.forPattern("dd MMMM YYYY").withLocale(Locale.ENGLISH);
    DateTime dt = dtf.parseDateTime("25 December 1985");
    System.out.println(dt.getDayOfWeek());
    

    【讨论】:

    • 线程“main”java.lang.IllegalArgumentException 中的异常:格式无效:“1985 年 12 月 25 日”在“1985 年 12 月”格式错误
    • 你的 Joda lib 版本是多少?
    【解决方案2】:

    参考http://joda-time.sourceforge.net/userguide.html

    getDayOfWeek()

    int iDoW = dt.getDayOfWeek();
    

    iDoW 可以在哪里取值(来自 DateTimeConstants 类)

    public static final int MONDAY = 1;
    public static final int TUESDAY = 2;
    public static final int WEDNESDAY = 3;
    public static final int THURSDAY = 4;
    public static final int FRIDAY = 5;
    public static final int SATURDAY = 6;
    public static final int SUNDAY = 7;
    

    dayOfWeek()

    DateTime.Property pDoW = dt.dayOfWeek();
    String strST = pDoW.getAsShortText(); // returns "Mon", "Tue", etc.
    String strT = pDoW.getAsText(); // returns "Monday", "Tuesday", etc.
    

    【讨论】:

    • 好的。但是如何解析字符串“1985 年 12 月 25 日?我的示例不起作用。它会引发异常。
    • 啊,我明白了,你从来没有说过抛出异常。
    猜你喜欢
    • 2014-04-08
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    相关资源
    最近更新 更多