Time Limits Range Joda

I am using the Joda Time library to parse a string in dateTime using the parseDateTime function in this library and noticed that the date range supported for this library is -292,269,054 to 292,277,023.

Does anyone know how to limit the date range using this library. Especially over the years (YYYY) to 9999?

Thanks.

+5
source share
4 answers

Interval Class

You can limit date ranges in Joda-Time with Interval .

You can then ask if the DateTime is within this interval / range.

+4
source

As noted in MadProgrammer , date range limitation is your job as an application developer. Joda-Time cannot know what you think is reasonable.

Bean Check

To handle this data validation task, you can find the helpful Bean Validation . Defined by JSR 303 ( spec 1.0 ) and JSR 349 ​​( specification 1.1) .

With Bean Validation, you can conveniently use annotations to define rules, such as the minimum and maximum values ​​for a specific member variable in a class.

+1
source

Joda-Time unexpectedly offers what you want (really?). The obvious solution is using LimitChronology . Example:

 DateTime min = new DateTime(2014, 1, 1, 0, 0); DateTime max = new DateTime(2015, 1, 1, 0, 0).minusMillis(1); Chronology chronology = LimitChronology.getInstance(ISOChronology.getInstance(), min, max); DateTime now = DateTime.now(chronology); System.out.println(now.toString(DateTimeFormat.fullDateTime())); // output: Donnerstag, 6. November 2014 19:08 Uhr MEZ DateTime test = new DateTime(1970, 1, 1, 0, 0).withChronology(chronology); System.out.println(test.toString(DateTimeFormat.fullDateTime())); // no exception! => output:  ,  .          :   Uhr MEZ test = now.withYear(1970); // IllegalArgumentException: // The resulting instant is below the supported minimum of // 2014-01-01T00:00:00.000+01:00 (ISOChronology[Europe/Berlin]) 

However, my advice should not use this feature.

The first reason is the inconvenience of using LimitChronology for each DateTime object in your program. You will probably have to change the architecture of the application to set up a central factory to create such exotic DateTime objects to make sure that you really haven't forgotten any object.

Another reason is the partial unreliability of the chronology in question. It cannot prevent the creation of instances of DateTime objects outside the supported limited range, but it creates strange formatted output (see the Example above).

Therefore, I suggest that you follow the recommendations of @MadProgrammer or @CharlieS, using Interval to check the range.

+1
source

I'm not sure, but you can test with this code

 DateTime startRange = new DateTime(2010, 1, 1, 12, 0, 0, 0); DateTime endRange = new DateTime(9999, 12, 31, 21, 59, 59, 59); Interval interval = new Interval(startRange, endRange); DateTime testRange = new DateTime(2014, 10, 30, 11, 0, 0, 0); System.out.println(interval.contains(testRange)); // returns true endRange = new DateTime(2014, 12, 31, 21, 59, 59, 59); testRange = new DateTime(9999, 10, 30, 11, 0, 0, 0); System.out.println(interval.contains(testRange)); // returns false 
0
source

Source: https://habr.com/ru/post/1205830/


All Articles