bardframework / jalali-date

Jalali (Shamsi) calenadar implementation in java for parsing, validating, manipulating, converting and formatting persian dates (like java 8 LocalDate(Time))
Apache License 2.0
5 stars 2 forks source link

Exception when subtracting days or weeks from LocalDateTimeJalali that crosses month boundary #2

Open amirh-khali opened 11 months ago

amirh-khali commented 11 months ago

Hello, I’m using your library for my project and appreciate your work. However, I encountered a problem when subtracting days or weeks from a LocalDateTimeJalali object that crosses a month boundary. For example, if I have a LocalDateTimeJalali object with the value of 1402-09-01T00:00:00, and I try to subtract more than one day (somehow it works fine with one day) or weeks from it, I get the following exception:

Exception in thread "main" java.time.DateTimeException: Invalid value for DayOfMonth (valid values 1 - 28/31): -6 at java.base/java.time.temporal.ValueRange.checkValidValue(ValueRange.java:319) at java.base/java.time.temporal.ChronoField.checkValidValue(ChronoField.java:718) at org.bardframework.time.LocalDateJalali.of(LocalDateJalali.java:244) at org.bardframework.time.LocalDateJalali.plusDays(LocalDateJalali.java:1497) at org.bardframework.time.LocalDateTimeJalali.plusDays(LocalDateTimeJalali.java:1300) at org.bardframework.time.LocalDateTimeJalali.minusDays(LocalDateTimeJalali.java:1498)

I hope you can fix this bug soon and release a new version of the library. Thank you for your attention and your great work.

v-zafari commented 11 months ago

Hi amir. Can you send example code that causes the error?

@amirh-khali

amirh-khali commented 11 months ago

Sure, here is one example of this situation:

fun main() {
    val now = LocalDateTimeJalali.now()
    println(now)
    println(now.minusDays(8))
    println(now.minusDays(9))
}

output:

1402-09-08T12:02:25.210208541
1402-08-30T12:02:25.210208541
Exception in thread "main" java.time.DateTimeException: Invalid value for DayOfMonth (valid values 1 - 28/31): -1
    at java.base/java.time.temporal.ValueRange.checkValidValue(ValueRange.java:319)
    at java.base/java.time.temporal.ChronoField.checkValidValue(ChronoField.java:718)
    at org.bardframework.time.LocalDateJalali.of(LocalDateJalali.java:244)
    at org.bardframework.time.LocalDateJalali.plusDays(LocalDateJalali.java:1497)
    at org.bardframework.time.LocalDateTimeJalali.plusDays(LocalDateTimeJalali.java:1300)
    at org.bardframework.time.LocalDateTimeJalali.minusDays(LocalDateTimeJalali.java:1498)
v-zafari commented 11 months ago

It was tested in version 2.6.27 and ran successfully without errors. Sample code and output:

public static void main(String[] args) {
    LocalDateTimeJalali now = LocalDateTimeJalali.now();
    System.out.println(now);
    System.out.println(now.minusDays(8));
    System.out.println(now.minusDays(9));
}
1402-09-10T16:02:24.551494400
1402-09-02T16:02:24.551494400
1402-09-01T16:02:24.551494400

Process finished with exit code 0
amirh-khali commented 10 months ago

the code I sent was wrong on that day of mouth. try this general one:

fun main() {
    val firstDayOfMonth = LocalDateTimeJalali.of(1402, 10, 1, 0, 0)
    println(firstDayOfMonth)
    println(firstDayOfMonth.minusDays(1))
    println(firstDayOfMonth.minusDays(2))
}
1402-10-01T00:00
1402-09-30T00:00
Exception in thread "main" java.time.DateTimeException: Invalid value for DayOfMonth (valid values 1 - 28/31): -1
    at java.base/java.time.temporal.ValueRange.checkValidValue(ValueRange.java:319)
    at java.base/java.time.temporal.ChronoField.checkValidValue(ChronoField.java:718)
    at org.bardframework.time.LocalDateJalali.of(LocalDateJalali.java:244)
    at org.bardframework.time.LocalDateJalali.plusDays(LocalDateJalali.java:1497)
    at org.bardframework.time.LocalDateTimeJalali.plusDays(LocalDateTimeJalali.java:1300)
    at org.bardframework.time.LocalDateTimeJalali.minusDays(LocalDateTimeJalali.java:1498)