...
If you want to localize a Date
instance, you'll need a Locale
and TimeZone
instance. The OFBiz framework makes the user's locale and time zone available throughout most of the execution path - usually in a context Map
. Call the UtilDateTime.toDateTimeFormat
method (leave the String argument null) method to get a DateFormat
instance. When you call DateFormat.format
, the class will compute the user's date and time using the immutable, GMT-referenced Date
and the information provided by the TimeZone
instance. The format of the date/time String
is determined controlled by the Locale
instance.
If you want to perform date/time arithmetic, you have two options: use the UtilDateTime.adjustTimestamp
method, or create a Calendar
instance by calling UtilDateTime.toCalendar
and adjust the date using that instance.
...
Different cultures use different calendars. Different regions can have different calendar rules, even when using the same calendar. The Calendar
class, when used properly, will accommodate all of these differences.
Assumptions to avoid:
- A year has 12 months
- The week number in your locale is the same week number in another locale
- The week always starts with Sunday
- Everyone uses a Gregorian calendar
Never Use Millisecond Arithmetic
Knowing that the Date
class uses milliseconds wraps a millisecond value makes it tempting to use millisecond arithmetic to perform date/time calculations. That approach will always fail - because it doesn't take into consideration different calendar systems, time zones, etc. OFBiz developers should never use millisecond arithmetic.