如何从 Java 8 日期 API 中的回历日期中减去回历年份
How to subtract Hijrah year from a Hijrah Date in Java 8 Date API
我要显示 Ramadan 2017 start and end dates. I tried writing code using the HijrahChronology
built into Java 8 and later, with HijrahDate
class.
import java.time.LocalDate;
import java.time.chrono.HijrahDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
public class Ramdan {
public static void main(String[] args) {
HijrahDate ramdanDate = HijrahDate.now().with(ChronoField.DAY_OF_MONTH, 1).with(ChronoField.MONTH_OF_YEAR, 9);
LocalDate ramdanStart = LocalDate.from(ramdanDate);
LocalDate ramdanEnd = LocalDate.from(ramdanDate.with(TemporalAdjusters.lastDayOfMonth()));
System.out.println("Ramdan 2017");
System.out.println(ramdanStart);
System.out.println(ramdanEnd);
}
}
但它显然打印出当年的日期,即 2018 年。
Output
Ramdan 2017
2018-05-16
2018-06-14
我尝试了很多方法,例如减去年数,或进行时间调整,但都无济于事。有人可以建议一种很酷的实现方式吗?
我不确定这是完美的方法,但这对我有用,而且看起来并不复杂:
ramdanDate = ramdanDate.with(ChronoField.YEAR, ramdanDate.get(ChronoField.YEAR) - 1);
在转换为 LocalDate
之前插入这一行,您的代码现在打印:
Ramdan 2017
2017-05-27
2017-06-24
编辑:
Suppose even if (in case you got a Gregorian year that overlaps with
two instances of Ramadan, finding both) is desired...I cannot think of
a way of achieving it. Can you suggest?
舌头伸直:
public static void printRamdanDates(int gregorianYear) {
LocalDate gregDate = LocalDate.ofYearDay(gregorianYear, 1);
HijrahDate ramdanDate = HijrahDate.from(gregDate)
.with(ChronoField.DAY_OF_MONTH, 1)
.with(ChronoField.MONTH_OF_YEAR, 9);
LocalDate ramdanStart = LocalDate.from(ramdanDate);
LocalDate ramdanEnd = LocalDate.from(ramdanDate.with(TemporalAdjusters.lastDayOfMonth()));
// if in previous Gregorian year, skip
while (ramdanEnd.getYear() < gregorianYear) {
ramdanDate = ramdanDate.with(ChronoField.YEAR, ramdanDate.get(ChronoField.YEAR) + 1);
ramdanStart = LocalDate.from(ramdanEnd);
ramdanEnd = LocalDate.from(ramdanDate.with(TemporalAdjusters.lastDayOfMonth()));
}
if (ramdanStart.getYear() > gregorianYear) { // in following Gregorian year
System.out.println("No Ramdan in " + gregorianYear);
} else {
System.out.println("Ramdan " + gregorianYear);
do {
System.out.println(ramdanStart);
System.out.println(ramdanEnd);
ramdanDate = ramdanDate.with(ChronoField.YEAR, ramdanDate.get(ChronoField.YEAR) + 1);
ramdanStart = LocalDate.from(ramdanDate);
ramdanEnd = LocalDate.from(ramdanDate.with(TemporalAdjusters.lastDayOfMonth()));
} while (ramdanStart.getYear() == gregorianYear);
}
}
如果我将 2017 提供给上述方法,它会给出与之前相同的结果:
printRamdanDates(2017);
输出:
Ramdan 2017
2017-05-27
2017-06-24
但是尝试,例如,2000 年或 2030 年斋月在 Gregorian/ISO 年发生两次。
printRamdanDates(2000);
printRamdanDates(2030);
输出:
Ramdan 2000
1999-12-09
2000-01-07
2000-11-27
2000-12-26
Ramdan 2030
2030-01-05
2030-02-03
2030-12-26
2031-01-23
我要显示 Ramadan 2017 start and end dates. I tried writing code using the HijrahChronology
built into Java 8 and later, with HijrahDate
class.
import java.time.LocalDate;
import java.time.chrono.HijrahDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
public class Ramdan {
public static void main(String[] args) {
HijrahDate ramdanDate = HijrahDate.now().with(ChronoField.DAY_OF_MONTH, 1).with(ChronoField.MONTH_OF_YEAR, 9);
LocalDate ramdanStart = LocalDate.from(ramdanDate);
LocalDate ramdanEnd = LocalDate.from(ramdanDate.with(TemporalAdjusters.lastDayOfMonth()));
System.out.println("Ramdan 2017");
System.out.println(ramdanStart);
System.out.println(ramdanEnd);
}
}
但它显然打印出当年的日期,即 2018 年。
Output
Ramdan 2017
2018-05-16
2018-06-14
我尝试了很多方法,例如减去年数,或进行时间调整,但都无济于事。有人可以建议一种很酷的实现方式吗?
我不确定这是完美的方法,但这对我有用,而且看起来并不复杂:
ramdanDate = ramdanDate.with(ChronoField.YEAR, ramdanDate.get(ChronoField.YEAR) - 1);
在转换为 LocalDate
之前插入这一行,您的代码现在打印:
Ramdan 2017
2017-05-27
2017-06-24
编辑:
Suppose even if (in case you got a Gregorian year that overlaps with two instances of Ramadan, finding both) is desired...I cannot think of a way of achieving it. Can you suggest?
舌头伸直:
public static void printRamdanDates(int gregorianYear) {
LocalDate gregDate = LocalDate.ofYearDay(gregorianYear, 1);
HijrahDate ramdanDate = HijrahDate.from(gregDate)
.with(ChronoField.DAY_OF_MONTH, 1)
.with(ChronoField.MONTH_OF_YEAR, 9);
LocalDate ramdanStart = LocalDate.from(ramdanDate);
LocalDate ramdanEnd = LocalDate.from(ramdanDate.with(TemporalAdjusters.lastDayOfMonth()));
// if in previous Gregorian year, skip
while (ramdanEnd.getYear() < gregorianYear) {
ramdanDate = ramdanDate.with(ChronoField.YEAR, ramdanDate.get(ChronoField.YEAR) + 1);
ramdanStart = LocalDate.from(ramdanEnd);
ramdanEnd = LocalDate.from(ramdanDate.with(TemporalAdjusters.lastDayOfMonth()));
}
if (ramdanStart.getYear() > gregorianYear) { // in following Gregorian year
System.out.println("No Ramdan in " + gregorianYear);
} else {
System.out.println("Ramdan " + gregorianYear);
do {
System.out.println(ramdanStart);
System.out.println(ramdanEnd);
ramdanDate = ramdanDate.with(ChronoField.YEAR, ramdanDate.get(ChronoField.YEAR) + 1);
ramdanStart = LocalDate.from(ramdanDate);
ramdanEnd = LocalDate.from(ramdanDate.with(TemporalAdjusters.lastDayOfMonth()));
} while (ramdanStart.getYear() == gregorianYear);
}
}
如果我将 2017 提供给上述方法,它会给出与之前相同的结果:
printRamdanDates(2017);
输出:
Ramdan 2017
2017-05-27
2017-06-24
但是尝试,例如,2000 年或 2030 年斋月在 Gregorian/ISO 年发生两次。
printRamdanDates(2000);
printRamdanDates(2030);
输出:
Ramdan 2000
1999-12-09
2000-01-07
2000-11-27
2000-12-26
Ramdan 2030
2030-01-05
2030-02-03
2030-12-26
2031-01-23