首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Joda time - IllegalFieldValueException: dayOfMonth的值30必须在[1,29]范围内

Joda time - IllegalFieldValueException: dayOfMonth的值30必须在[1,29]范围内
EN

Stack Overflow用户
提问于 2017-11-28 18:51:22
回答 2查看 1.4K关注 0票数 1

有一个对某些外部系统的调用,它返回hijri格式的日期。在生产环境中,我们将其作为'30-02-1436'接收。

考虑一下将hijri转换为公历日期的代码片段,但它无法做到这一点。

代码语言:javascript
复制
    public static String convertHijriToGregorianDate(String hijriDate){
        Chronology iso = ISOChronology.getInstanceUTC(); // get the ISO standard chronology which we follow now
        Chronology hijri = IslamicChronology.getInstanceUTC(); // the hijri based chronology
        //String hijriDate = "19-08-1435"; // example/format which is received as a parameter in the method from response

        if(null != hijriDate && !hijriDate.isEmpty()){
            String[] parts = hijriDate.trim().split("-"); // you will get an array of size 3 by splitting with regex '-'
            String hijriYear = parts[2];
            String hijriMonth = parts[1];
            String hijriDay = parts[0];

            // Construct the Local Date corresponding to the hijri chronology
            LocalDate todayHijri = new LocalDate(Integer.parseInt(hijriYear), Integer.parseInt(hijriMonth), Integer.parseInt(hijriDay), hijri);

            // Construct the Local Date corresponding to the iso chronology based on the hijri date
            LocalDate todayIso = new LocalDate(todayHijri.toDateTimeAtStartOfDay(), iso);
            return String.valueOf(todayIso);
        }
        return null;
    }

请指出代码的错误之处。我们在堆栈跟踪中得到了这个问题-

org.joda.time.IllegalFieldValueException:在org.joda.time.field.FieldUtils.verifyValueBounds(FieldUtils.java:252) at org.joda.time.chrono.BasicChronology.getDateMidnightMillis(BasicChronology.java:632) at org.joda.time.chrono.BasicChronology.getDateTimeMillis0(BasicChronology.java:186) at org.joda时,dayOfMonth的值30必须在1,29的范围内。time.chrono.BasicChronology.getDateTimeMillis(BasicChronology.java:160) at org.joda.time.chrono.LimitChronology.getDateTimeMillis(LimitChronology.java:177) at org.joda.time.chrono.BasicChronology.getDateTimeMillis(BasicChronology.java:155) at org.joda.time.LocalDate。(LocalDate.java:457)

使用的Joda时间版本为2.9.1

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-28 19:29:59

我已经通过以下表达式检查了Joda-Time中所有可用的闰年模式(=4

代码语言:javascript
复制
Chronology hijri = 
    IslamicChronology.getInstance(DateTimeZone.UTC, IslamicChronology.LEAP_YEAR_15_BASED);

不幸的是,任何东西都不适合你。然而,沙特阿拉伯的umalqura-认为'30-02-1436‘是有效的。它对应于公历日期2014-12-22。

Joda Time不支持这种伊斯兰日历变体,因此除非您愿意迁移到包含umalqura变体的-8,否则您无法在此处执行任何操作。

代码语言:javascript
复制
System.out.println(HijrahDate.of(1436, 2, 30)); // Hijrah-umalqura AH 1436-02-30

DateTimeFormatter fh =
    DateTimeFormatter.ofPattern(
        "dd-MM-yyyy",
        Locale.forLanguageTag("en")
    ).withChronology(HijrahChronology.INSTANCE);
System.out.println(HijrahDate.from(fh.parse(input))); 
// Hijrah-umalqura AH 1436-02-30
System.out.println(LocalDate.from(HijrahDate.from(fh.parse(input)))); 
// 2014-12-22

注意:不幸的是,unicode扩展Locale.forLanguageTag("en-u-ca-islamic-umalqura")在我的实验中似乎不起作用,所以显式地指定时间顺序是必要的。

替代方案:

如果您不能迁移到Java-8,或者甚至需要伊斯兰日历的更多特性(例如,Joda-Time中使用的其他变体),那么您也可以尝试我的库Time4J

代码语言:javascript
复制
String input = "30-02-1436";
ChronoFormatter<HijriCalendar> hf =
    ChronoFormatter.ofPattern(
        "dd-MM-yyyy",
        PatternType.CLDR,
        Locale.ROOT,
        HijriCalendar.family()
    ).withCalendarVariant(HijriCalendar.VARIANT_UMALQURA).with(Leniency.STRICT);
System.out.println(hf.parse(input)); // AH-1436-02-30[islamic-umalqura]
System.out.println(hf.parse(input).transform(PlainDate.axis())); // 2014-12-22
票数 2
EN

Stack Overflow用户

发布于 2019-03-01 04:31:50

该日期被解释为2月30日,但本月的天数必须少于30天。这解释了错误消息。因此,请更正输入,使其与可能的日期相对应。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47529720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档