为什么要将回历与 iso 日期进行比较,您需要将 DateTime 包装在 LocalDateTime 中? (乔达)

Why in order to compare hijri with iso dates you need to wrap a DateTime in LocalDateTime? (Joda)

我正在尝试将 ISO(公历)日期转换为回历(伊斯兰)日期,并将第一个日期与回历日期进行比较。

我所做的但不起作用的是:

    Chronology iso = ISOChronology.getInstanceUTC();
    Chronology hijri = IslamicChronology.getInstanceUTC();

    DateTime dtISO = new DateTime("2015-03-12T10:04:43", iso);

    System.out.println("dtISO: " + dtISO);
    DateTime toHijri = new DateTime(dtISO, hijri);
    System.out.println("toHijri: " + toHijri); // 1436-05-21T10:04:43.000Z
    DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH-mm");

    DateTime hijriDt = new DateTime(formatter.parseDateTime("1442-05-21 10-10"), iso);

    if (hijriDt.isAfter(toHijri)) {
        System.out.println("hijriDt date is after toHijri date");
    }
    if (hijriDt.isBefore(toHijri)) {
        System.out.println("hijriDt date is before toHijri date");
    }
    if (hijriDt.isEqual(toHijri)) {
        System.out.println("hijriDt date is equal toHijri date");
    }

无论回历日期是什么,它总是 returns "hijriDt date is before toHijri date".

现在,当我在做实验时,我尝试了这个:

    Chronology iso = ISOChronology.getInstanceUTC();
    Chronology hijri = IslamicChronology.getInstanceUTC();

    LocalDateTime toHijri = new LocalDateTime(new DateTime("2015-03-12T10:04:43", iso), hijri);
    System.out.println("toHijri: " + toHijri); // 1436-05-21T10:04:43.000Z
    DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY-MM-dd HH-mm");

    LocalDateTime hijriDt = new LocalDateTime(formatter.parseDateTime("1442-05-21 10-10"), iso);

    if (hijriDt.isAfter(toHijri)) {
        System.out.println("hijriDt date is after toHijri date");
    }
    if (hijriDt.isBefore(toHijri)) {
        System.out.println("hijriDt date is before toHijri date");
    }
    if (hijriDt.isEqual(toHijri)) {
        System.out.println("hijriDt date is equal toHijri date");
    }

这很有魅力。注意 toHijri 日期是如何构建的: LocalDateTime toHijri = new LocalDateTime(new DateTime("2015-03-12T10:04:43", iso), 回历);

DateTime 实例包含在 LocalDateTime 中。

为什么会这样?为什么第一个块不起作用?

这不是你想要的:

DateTime hijriDt = new DateTime(formatter.parseDateTime("1442-05-21 10-10"), iso);

这不是使用回历 任何地方:

  • 您的格式化程序正在使用 ISO(默认)
  • 您正在解析 ISO 1442 年的日期,这不是您想要的
  • 然后你 "converting" 进入 ISO 日历(它已经是)
  • 您的变量名称表明您希望它出现在回历中,但事实并非如此

我怀疑你想要:

DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH-mm")
    .withChronology(hijri);
DateTime hijriDt = formatter.parseDateTime("1442-05-21 10-10");