SourceForge.net Logo
Version: 0.27 - Last update: 2004-05-30 Home Page - SourceForge Project Page - Contact

Jodd
  Overview
  News
  Download
  License
  References

Development
  Using Jodd
  Javadoc
  JUnit report

Community
  Contribute
  Report a bug

More reading
  Milestone 0.30



Top 25%

BSD
 

JDateTime

JDateTime provides precise, functional and configurable date and time manipulation. It has been designed to have all of the functionalities of similar date/ti,e classes, but to be simpler to use, and yet enhanced and fast as possible. JDateTime should satisfy the most of date/time needs.

JDateTime uses Astronomical Julian Date Numbers to store date/time information as Julian Dates. Precision of JDateTime is set to 1 millisecond.

Initialization

JDateTime can be initialized in many ways. Default initialization sets the current date and time. Further, it may be set to a specific date and/or time. Number of milliseconds since 1970 (e.g. System.currentTimeMillis) may also be used. Date and time of JDateTime object can also be set by each field independently, if there is a need for that. And at the end, JDateTime can be initialized or set from instance of any available Java date/time class, as well as from String, as it will be shown later.


JDateTime initialization
 
JDateTime jdt = new JDateTime();	// current date/time
jdt.set(1999, 03, 21, 19, 50, 17.321);	// specific date/time
jdt.set(2001, 07, 13);			// specific date
jdt.setYear(2003);			// sets just specific year
jdt.set(System.currentTimeMillis());	// milliseconds

Java time/date classes

JDateTime, as said above, can be initialized from an object of any available Java date/time class:
  • java.sql.Timestamp
  • java.util.GregorianCalendar
  • java.util.Calendar
  • java.sql.Date
  • java.util.Date
But JDateTime can also act as a factory for above classes: it can return a new instance of any of above classes. There is also a possibility to store date/time values to an existing object of above classes.

JDateTime and Java classes
 
jdt = new JDateTime(CalendarInstance);	// from calendar
jdt.loadFrom(TimestampInstance);	// date/time from calendar
Calendar c = (Calendar) jdt.getInstance(Calendar.class);	// new calendar instance
Calendar c = jdt.getCalendarInstance();	// shortcut for previous
jdt.storeTo(c);				// store to existing calendar

Besides above default Java date/time classes, it is possible to add so-called 'converters' for any other custom date/time class.

Rolling

JDateTime is very flexible and precise with adding date/time. Values can be added for all date/time elements at once, or for one element at time. Any value can be used for adding, both positive and negative.

JDateTime rolling
 
jdt.add(1, 2, 3, 4, 5, 6.6);		// add date and time
jdt.add(0, 120, 0);			// add 120 months
jdt.addMonth(120);			// same as previous
jdt.addHour(1234);			// add 1234 hours

JDateTime is aware of problem that may happens when adding month and years, since months are not equally long. By default it is taken care of this, however, this fixing can be turned off if need.

Strings

One of the power features of JDateTime is a possibility to set time from and to get time as a String. There is a default date/time format, but it is also possible to specify custom format, both globally or just for one method call. JdtFormater implementations may be used for custom date/time string setting and getting. Default formatter uses enhanced set of patterns defined by ISCO 8601 standard.

JDateTime and Strings
 
jdt.get();				// get date/time in default format
jdt.get("YYYY.MM.DD");			// get date in specified format
jdt.set("01-01.1975", "DD-MM.YYYY");	// set date from specified format

JDateTime provides an options to use day and month names and abbreviations in any language. Jodd provides few languages itself.

Weeks

JDateTime provides various week definition, used for calculating weeks of year or month.


http://jodd.sourceforge.net
najgor at users.sourceforge.net