To use the user time zone, date and time format¶
Items
- Prerequisites
- To save date and time sent from the screen in DB
- To save date sent from the screen in DB
- To display date and time saved in DB on user screen
- To display date saved in DB on user screen
- To create dates for 3 days of user time zone starting from today at the client side and sending the last date to the server side
Prerequisites¶
- Various Setting Values
Set Item Setting Value User Time zone (GMT+09:00) Japan/Tokyo System Time zone (GMT+00:00) UTC Date and time format English Format Date (Standard Display) MMM d, yyyy Date (Simplified Display) MMM d Date (Input) yyyy/MM/dd Time (Standard Display) h:mm a Time (Timestamp Display) h:mm:ss a Time (Input) | HH:mm
Table Definition
CREATE TABLE example_table ( user_cd VARCHAR(100) NOT NULL, update_date TIMESTAMP NOT NULL, PRIMARY KEY (user_cd) );DB Access
Assume that the following class is provided for updating and referring example_table.Model class for saving the record information of example_table.ExampleTableModelClass for updating and referring example_table.SampleService
To save date and time sent from the screen in DB¶
The flow of analyzing the date and time entered from the screen, at the server side and saving it in DB is explained.It is assumed that the user has entered the following values in the date and time format in screen and sent to the server.2012/09/19 03:46Server side program is as follows.import java.util.Date; import jp.co.intra_mart.foundation.context.Contexts; import jp.co.intra_mart.foundation.context.model.AccountContext; import jp.co.intra_mart.foundation.i18n.datetime.format.AccountDateTimeFormatter; import jp.co.intra_mart.foundation.i18n.datetime.format.DateTimeFormatIds; import jp.co.intra_mart.foundation.i18n.datetime.format.DateTimeFormatterException; import jp.co.intra_mart.foundation.i18n.sa.sample.model.ExampleTableModel; import jp.co.intra_mart.foundation.i18n.sa.sample.service.SampleService; import jp.co.intra_mart.foundation.i18n.sa.sample.service.SampleServiceException; /** * Sample */ public class SampleDateTime { /** * Save date and time sent from screen in DB. * * @param inputDate Date and time string sent from screen * @throws DateTimeFormatterException * @throws SampleServiceException */ public void sample(final String inputDate) throws DateTimeFormatterException, SampleServiceException { /* * 1. Analyze date and time string sent from screen. */ final Date date = AccountDateTimeFormatter.parse(inputDate, Date.class, DateTimeFormatIds.IM_DATETIME_FORMAT_DATE_INPUT, DateTimeFormatIds.IM_DATETIME_FORMAT_TIME_INPUT,); /* * 2. Save date and time in DB using model. */ final ExampleTableModel model = new ExampleTableModel(); model.setUserCd(Contexts.get(AccountContext.class).getUserCd()); model.setDate(date); SampleService.getInstance().update(model); } }Use AccountDateTimeFormatter to analyze date and time string which is in accordance with the input format of the user.AccountDateTimeFormatter analyses using time zone of logged in user.Save date and time data which is changed to the system default time zone in DB.Since Date type object does not have time zone, date and time data which is changed to the time zone of Java VM is saved in TIMESTAMP type column of DB.Java VM time zone and system default time zone are the same.
To save date sent from the screen in DB¶
The flow of analyzing the date entered from the screen at the server side up to saving it in DB is explained.Warning
Even though only the date is entered from the screen, generally time should also be considered; as given in the below example.(Example) The deadline of a transportation-expenses applicationThe meaning of “Deadline” means, the time up to 23:59:59 (Or, business hours).For example, if the deadline is 19th Sep of Japan Head Office (GMT+09:00), transportation-expenses application should be finished by 5 P.M. of 19th Sep at the Honolulu branch office (GMT-10:00)It is assumed that the user has entered the following value in the date input format and sent to the server.2012/09/19Server side program is as follows.import java.util.Date; import jp.co.intra_mart.foundation.context.Contexts; import jp.co.intra_mart.foundation.context.model.AccountContext; import jp.co.intra_mart.foundation.i18n.datetime.format.AccountDateTimeFormatter; import jp.co.intra_mart.foundation.i18n.datetime.format.DateTimeFormatIds; import jp.co.intra_mart.foundation.i18n.datetime.format.DateTimeFormatterException; import jp.co.intra_mart.foundation.i18n.sa.sample.model.ExampleTableModel; import jp.co.intra_mart.foundation.i18n.sa.sample.service.SampleService; import jp.co.intra_mart.foundation.i18n.sa.sample.service.SampleServiceException; /** * Sample */ public class SampleDateTime { /** * Save date and time sent from screen in DB. * * @param inputDate Date and time string sent from screen * @throws DateTimeFormatterException * @throws SampleServiceException */ public void sample(final String inputDate) throws DateTimeFormatterException, SampleServiceException { /* * 1. Analyze date and time string sent from screen. */ final Date date = AccountDateTimeFormatter.parse(inputDate, Date.class, DateTimeFormatIds.IM_DATETIME_FORMAT_DATE_INPUT, DateTimeFormatIds.IM_DATETIME_FORMAT_TIME_INPUT,); /* * 2. Save date and time in DB using model. */ final ExampleTableModel model = new ExampleTableModel(); model.setUserCd(Contexts.get(AccountContext.class).getUserCd()); model.setDate(date); SampleService.getInstance().update(model); } }In case of date string, time part is analyzed as “00:00:00”.Saving in DB is similar to “To save date sent from the screen in DB”.
To display date and time saved in DB on user screen¶
The flow of adjusting the date and time which is saved in DB; to the date and time string, using date and time display format and user time zone up to displaying it on the screen is explained.Server side program is as follows.import jp.co.intra_mart.foundation.context.Contexts; import jp.co.intra_mart.foundation.context.model.AccountContext; import jp.co.intra_mart.foundation.i18n.datetime.format.AccountDateTimeFormatter; import jp.co.intra_mart.foundation.i18n.datetime.format.DateTimeFormatIds; import jp.co.intra_mart.foundation.i18n.sa.sample.model.ExampleTableModel; import jp.co.intra_mart.foundation.i18n.sa.sample.service.SampleService; import jp.co.intra_mart.foundation.i18n.sa.sample.service.SampleServiceException; /** * Sample */ public class SampleDateTimeView { /** * Display the date and time stored in DB on the user screen. * * @return String Date and time string * @throws SampleServiceException */ public String sample() throws SampleServiceException { /* * 1. Fetch date and time from DB. */ final ExampleTableModel model = SampleService.getInstance().get(Contexts.get(AccountContext.class).getUserCd()); /* * 2. Fix date and time string. */ return AccountDateTimeFormatter.format(model.getDate(), DateTimeFormatIds.IM_DATETIME_FORMAT_DATE_STANDARD, DateTimeFormatIds.IM_DATETIME_FORMAT_TIME_STANDARD); } }Date and time which is changed to system default time zone is saved in DB.Use AccountDateTimeFormatter to adjust to the date and time string which is in accordance with the display format of the user.AccountDateTimeFormatter calculates the time according to the time zone of the logged in user.Following result is obtained.Sep 19, 2012 3:46 AM
To display date saved in DB on user screen¶
The flow of adjusting the date which is saved in DB to the date string, using date display format and user time zone up to displaying it on the screen is explained.Server side program is as follows.import jp.co.intra_mart.foundation.context.Contexts; import jp.co.intra_mart.foundation.context.model.AccountContext; import jp.co.intra_mart.foundation.i18n.datetime.format.AccountDateTimeFormatter; import jp.co.intra_mart.foundation.i18n.datetime.format.DateTimeFormatIds; import jp.co.intra_mart.foundation.i18n.sa.sample.model.ExampleTableModel; import jp.co.intra_mart.foundation.i18n.sa.sample.service.SampleService; import jp.co.intra_mart.foundation.i18n.sa.sample.service.SampleServiceException; /** * Sample */ public class SampleDateView { /** * Display the date saved in DB on the user screen. * * @return String Date string * @throws SampleServiceException */ public String sample() throws SampleServiceException { /* * 1. Fetch date and time from DB. */ final ExampleTableModel model = SampleService.getInstance().get(Contexts.get(AccountContext.class).getUserCd()); /* * 2. Fix date and time string. */ return AccountDateTimeFormatter.format(model.getDate(), DateTimeFormatIds.IM_DATETIME_FORMAT_DATE_STANDARD); } }Following result is obtained.Sep 19, 2012
To create dates for 3 days of user time zone starting from today at the client side and sending the last date to the server side¶
Understand the important points for handling the date and time data by csjs through this sample.
1. To fetch “Today” of user time zone¶
User time zone is the time zone registered in intra-mart Accel Platform by the user.Use ImDate provided by intra-mart Accel Platform to fetch the “Today” in user time zone by csjs.Warning
Do not use new Date of csjs for fetching “Today” in user time zone.newDate of csjs returns the current date and time data in time zone of client OS. However, user time zone may not necessarily match with the client OS time zone.<script type="text/javascript" src="im_i18n/timezone/im_date_timezone.js"></script> <script type="text/javascript"> var firstDate = ImDate.now(); </script>
2. To generate dates of 3 days starting from today¶
Method provided in Date as the standard can be used.var dateArray = new Array(); var date = firstDate; for (var i = 0; i < 3; i++) { dateArray[i] = date; date.setDate(date.getDate() + 1); }
3. To send latest date to the server¶
Create date string from date value.var lastDate = dateArray[2]; var lastDateStr = lastDate.getFullYear() + "-" + (lastDate.getMonth() + 1) + "-" + lastDate.getDate();Warning
Do not send Epoch milliseconds of Date created by ImDate.now().The Date which is returned by ImDate.now() has date time seconds of “Today” in user time zone. However, Epoch milliseconds are not necessarily right.The reason is that the Date is created at the client side.The value calculated according to the user time zone may not match with the Epoch milliseconds of Date which is calculated according to the client OS time zone.Warning
Use DateTime when creating date and time data of user time zone at the server side. Do not use Date only.It is important to consider the time difference between user’s time zones to calculate the Date according to the system default time zone.DateTime can be calculated according to the specified time zone.jp.co.intra_mart.foundation.i18n.datetime.DateTime dateTime = new jp.co.intra_mart.foundation.i18n.datetime.DateTime(Contexts.get(AccountContext.class).getTimeZone(), 1996, Calendar.SEPTEMBER, 19, 3, 47, 0);
4. To create Date from date string sent from the client side¶
Server side program is as follows.import java.sql.Date; import java.util.TimeZone; import javax.servlet.http.HttpServletRequest; import jp.co.intra_mart.foundation.context.Contexts; import jp.co.intra_mart.foundation.context.model.AccountContext; import jp.co.intra_mart.foundation.i18n.datetime.format.DateTimeFormatter; import jp.co.intra_mart.foundation.i18n.datetime.format.DateTimeFormatterException; /** * Sample */ public class SampleDTF { /** * Generate Date from date string sent from client side. * @param request Request * @throws DateTimeFormatterException */ public void sample(final HttpServletRequest request) throws DateTimeFormatterException { /* * 1. Fetch date string sent from client side. */ final String inputDateStr = request.getParameter("inputDate"); /* * 2. Generate Date from date string using DateTimeFormatter. */ final TimeZone userTimeZone = Contexts.get(AccountContext.class).getTimeZone(); final DateTimeFormatter formatter = DateTimeFormatter.withPattern("yyyy-MM-dd"); formatter.setTimeZone(userTimeZone); final Date inputDate = formatter.parse(inputDateStr, Date.class); } }Use DateTimeFormatter, when date and time string is not according to the date and time input format.DateTimeFormatter can specify the format pattern directly.DateTimeFormatter analyses using system default time zone.Set the user time zone before executing parse method when analyzing using user time zone.