JodaTime Extension

Joda-Time, as said in its website, "provides a quality replacement for the Java date and time classes". It contains lots of classes to work with dates, times, timezones and an integration with Hibernate among other functionalities.

op4j-jodatime is an extension that gives you access to a bunch of functions related to jodatime classes in an op4j way. The functions in this extension include:

  • Conversions from BaseDateTime to String
  • Conversions from Calendar to DateTime
  • Conversions from Date to DateMidnight
  • Creation of Period from Calendar objects
  • Creation of Interval from String objects
  • ... and many more

Function classes

The extension comprises the following function hub class:

  • FnJodaString: contains functions related to the conversion from several input types into String
  • FnDateMidnight: functions related to the conversion into DateMidnight
  • FnDateTime: functions related to the conversion to FnDateTime
  • FnMutableDateTime: functions returning a MutableDateTime as its output
  • FnInterval: contains functions returning an Interval as its output
  • FnLocalDate: deals with the conversion from several input types into LocalDate
  • FnLocalTime: its functions generate a LocalTime as its output
  • FnPeriod: contains a set of functions whose output is Period
  • FnJodaTimeUtils: is a shortcut to all of the methods included in the classes above. This way,
List<DateTime> targets = new ArrayList<DateTime>();
targets.add(new DateTime());
List<String> result1 = Op.onList(targets).forEach().exec(FnJodaString.baseDateTimeToStr(FormatType.PATTERN, 
  "yyyy-MM-DD_HH:mm:ss:SS")).get();
  
String pattern = "dd/mm/yyyy-HH:mm";
String asStr = "24/12/2000-15:03";
DateMidnight result2 = Op.on(asStr).exec(FnDateMidnight.strToDateMidnight(pattern)).get();

is equivalent to

List<DateTime> targets = new ArrayList<DateTime>();
targets.add(new DateTime());
List<String> result1 = Op.onList(targets).forEach().exec(FnJodaTimeUtils.baseDateTimeToStr(FormatType.PATTERN, 
  "yyyy-MM-DD_HH:mm:ss:SS")).get();
  
String pattern = "dd/mm/yyyy-HH:mm";
String asStr = "24/12/2000-15:03";
DateMidnight result2 = Op.on(asStr).exec(FnJodaTimeUtils.strToDateMidnight(pattern)).get();

Use

This section includes some examples of the use of the classes listed above. To learn about the methods available, visit the pages:

List<DateMidnight> targets = new ArrayList<DateMidnight>();
targets.add(new DateMidnight());
List<String> result = Op.onList(targets).forEach().exec(FnJodaString
  .baseDateTimeToStr(FormatType.STYLE, "MM", Locale.UK.toString())).get(); 
    
/* *** */
    
Timestamp timestamp = new Timestamp(new Date().getTime());
LocalTime result = Op.on(timestamp).exec(FnLocalTime.timestampToLocalTime(
  DateTimeZone.getDefault())).get();
    
/* *** */

Interval result = Op.on(Arrays.asList("1977", "5", "2", "23", "12", "2", "34",
  "1980", "8", "5", "12", "12", "12", "4"))
  .exec(FnInterval.strFieldCollectionToInterval(DateTimeZone.getDefault()))
  .get();

/* *** */
     
Interval result = Op.on(Arrays.asList("2000/02/09", "2001/09/05"))
  .exec(FnInterval.strFieldCollectionToInterval("yyyy/mm/dd", DateTimeZone.getDefault())).get();

/* *** */
 
Long start = Long.valueOf(1000);
Long end = Long.valueOf(1000000);    
Interval result = Op.on(new Long[] {start, end})
  .exec(FnInterval.longFieldArrayToInterval(GJChronology.getInstance()))
  .get();