乔达时间错误还是我的错误? (Java Joda时间日期为字符串解析) [英] Joda Time bug or my mistake? (Java Joda Time dates as strings parsing)

查看:159
本文介绍了乔达时间错误还是我的错误? (Java Joda时间日期为字符串解析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用JodaTime年表 IslamicChronology 解析一个日期,所以写了一个小例子来证明我的问题。

so I was having a problem parsing a date, using the JodaTime chronology IslamicChronology so wrote a small example to demonstrate my problem.

这里是代码:

import org.joda.time.Chronology;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.chrono.IslamicChronology;

import java.util.Date;

/**
 * Test
 */
public class Test
{
    public static void main(String[] args)
    {
        Date now = new Date();

        String format = "dd MMM yyyy";
        Chronology calendarSystem = IslamicChronology.getInstance();
        DateTimeFormatter formatter = DateTimeFormat.forPattern(format).withChronology(calendarSystem);

        String nowAsString = formatter.print(now.getTime());

        System.out.println("nowAsString = " + nowAsString);

        long parsedNowTs = formatter.parseMillis(nowAsString);

        String parsedNowTsAsString = formatter.print(parsedNowTs);
    }
}

输出:

nowAsString = 16 10 1430
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "16 10 1430" is malformed at "10 1430"
    at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:634)
    at test.Test.main(Test.java:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    ...

我在想,问题是月名是数字,但我是对的吗?有什么建议吗

I'm thinking the problem is that the month name is numeric but am I right? Anyone got any suggestions? This cannot be recreated if the chronology is gregorian.

提前感谢

推荐答案

如果有人感兴趣...我找到了一个工作:

If anyone's interested...I've found a work around:


  • 创建一个新类例如 IslamicChronologyWithNames ,它在package org.joda.time.DateTimeZone 中委托给 IslamicChronology 的实例

  • 修改一种方法; assemble(Fields fields):调用委托的方法,然后将fields.monthOfYear(和可能的dayOfWeek)设置为您自己的子类 BasicMonthOfYearDateTimeField

  • BasicMonthOfYearDateTimeField 的子类可以在属性文件中查找月份的日期(如果DayOfWeek ...)名称。子类需要在包 org.joda.time.chrono 中,以便能够扩展 BasicMonthOfYearDateTimeField

  • Create a new class e.g. IslamicChronologyWithNames which delegates to an instance of IslamicChronology in package org.joda.time.DateTimeZone
  • Modify the one method; assemble(Fields fields): call the delegate's method and then set fields.monthOfYear (and possibly dayOfWeek) to you own subclass of BasicMonthOfYearDateTimeField
  • The subclass of BasicMonthOfYearDateTimeField can then lookup in property files names for the month's (or day's if DayOfWeek...) name. Subclass needs to be in package org.joda.time.chrono to be able to extend BasicMonthOfYearDateTimeField.

仍然有一个问题,Joda时间似乎验证您正在解析的日期,然后调用子类的方法,如 getAsText(int fieldValue,Locale locale)并且因为它不知道您的类返回的月份名称,验证失败,因此从不调用方法。我的解决方法是在这个类中有一个静态方法,将一个日期作为一个字符串与伊朗的月份名称一起转换为一个具有格式的英文月份名称的字符串。所以在调用 parseDateTime()之前,调用静态方法,然后日期字符串通过验证。然后,在 convertText()方法中处理伊斯兰月份名称,而不是在子类中使用默认的gregorian实现:

There is still an issue that Joda time seems to validate the date you are parsing before calling methods of subclass like getAsText(int fieldValue, Locale locale) and because it has no knowledge of the month names that your class returns, fails validation and so never calls the methods. My work-around was to have a static method in this class which converts a date as a string with islamic month names into a date as a string with gregorian english month names. So before calling parseDateTime(), call the static method and then the date string passes validation. Then, instead of processing Islamic month names in the convertText() method, use the default gregorian implementation inside your subclass:

protected int convertText(String text, Locale locale)
{
    return GJLocaleSymbols.forLocale(locale).monthOfYearTextToValue(text);
}

应该工作!希望对任何有同样问题的人都有意义。

That should work! Hope it makes sense for anyone who has the same problem.

这篇关于乔达时间错误还是我的错误? (Java Joda时间日期为字符串解析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆