TimeService component
TimeService offers an interface to retrive dates and times considering TimeZone user preferences. TimeZone represents a time zone offset, and also figures out daylight savings.
TimeService is included at kernel. Thus, when importing org.sakaiproject.time.cover.TimeService or org.sakaiproject.time.api.TimeService (depending on if we are using the static cover or a dependecy injection), it is needed to include the following dependency in pom.xml:
<dependency> <groupId>org.sakaiproject.kernel</groupId> <artifactId>sakai-kernel-api</artifactId> </dependency>
One of the main uses of TimeService for i18n is to specify user preferred TimeZone before to format dates using java.text.DateFormat. Once a DateFormat has been retrieved, the next step will be to set user preferred TimeZone invoking TimeService:
DateFormat df = DateFormat.getInstance(DateFormat.MEDIUM, DateFormat.SHORT, new ResourceLoader().getLocale()); df.setTimeZone(TimeService.getLocalTimeZone());
Use getDateInstance to get the normal date format for that country. There are other static factory methods available. Use getTimeInstance to get the time format for that country. Use getDateTimeInstance to get a date and time format. You can pass in different options to these factory methods to control the length of the result; from SHORT to MEDIUM to LONG to FULL. The exact result depends on the locale, but generally:
- SHORT is completely numeric, such as 12.13.52 or 3:30pm
- MEDIUM is longer, such as Jan 12, 1952
- LONG is longer, such as January 12, 1952 or 3:30:32pm
- FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.
For more information about java.text.DateFormat, please, visit http://download.oracle.com/javase/6/docs/api/java/text/DateFormat.html.