- I need to give support for different timezone for my application.
- For inserting my JVM has UTC time as default so hibernate inserting created and updated date as UTC time with the help of @CreationTimestamp and @UpdateTimestamp which refers the JVM timezone.
- Each request is coming from the different timezone. based on the timezone I need to parse the UTC time to that specific timezone.
- I don't want to parse date manually for all records. is there any option in hibernate like if I specify the timezone while fetching. so that I can get the created and updated date as per given timezone (timezone are dynamic for fetching).
Asked
Active
Viewed 1,477 times
1

Ashok Kumar N
- 573
- 6
- 23
-
please look at this link also https://stackoverflow.com/questions/46493135/can-hibernate-convert-date-field-into-given-timezone-at-transaction-or-session-l – Ashok Kumar N Dec 12 '17 at 15:24
2 Answers
3
There are already several threads regarding the best practices with datetime, e.g. Daylight saving time and time zone best practices
I would suggest the following:
- always handle all datetimes as UTC in the backend and DB.
- transport the datetime to your clients as UTC.
- convert the UTC datetime to localdatetimezone in the client. For displaying and updating purposes.
From my experience it's best to let the client handle all local datetime/zone conversions and commit onto using UTC for all communication and backend usecases.
If you want to dump the date directly into a webpage you can use a js-library like http://momentjs.com/ to convert it to a locale datetime.

Florian Bronder
- 123
- 7
-
currently, i am planning to implement the steps of what you said for UI and for rest service I need to supply date as per given timezone – Ashok Kumar N Dec 12 '17 at 15:30
-
Here you find a thread regarding localdatetimezone conversions: https://stackoverflow.com/a/34605826/2067875 So, after getting the requested timezone in the rest-request you can convert your answer to required timezone with this. – Florian Bronder Dec 12 '17 at 15:40
0
You can create Utility (Generic methods) to convert date with timezone below some example to convert.
public static Date buildUTCDate(String dateString) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat(SecureCareConstant.SQL_TIMESTAMP_FORMAT);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.parse(dateString);
}
public static String dateToString(Date date) {
return new SimpleDateFormat(SecureCareConstant.SQL_TIMESTAMP_FORMAT).format(date);
}
public static Date buildUTCDate(Date date) {
SimpleDateFormat fromDateFormat = new SimpleDateFormat(SecureCareConstant.SQL_TIMESTAMP_FORMAT);
SimpleDateFormat toDateFormat = new SimpleDateFormat(SecureCareConstant.SQL_TIMESTAMP_FORMAT);
toDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String dateString = new SimpleDateFormat(SecureCareConstant.SQL_TIMESTAMP_FORMAT).format(date);
try {
return fromDateFormat.parse(toDateFormat.format(fromDateFormat.parse(dateString)));
} catch (ParseException e) {
LOGGER.error("ParseException in buildUTCDate()", e);
}
return null;
}
public static Date getCurrentTimeZoneDate(final Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
TimeZone z = c.getTimeZone();
int offset = z.getRawOffset();
if (z.inDaylightTime(new Date())) {
offset = offset + z.getDSTSavings();
}
int offsetHrs = offset / 1000 / 60 / 60;
int offsetMins = offset / 1000 / 60 % 60;
c.add(Calendar.HOUR_OF_DAY, (+offsetHrs));
c.add(Calendar.MINUTE, (+offsetMins));
return c.getTime();
}
public static String toLocalTime(Date dateUTC) {
if (dateUTC == null) {
return StringUtils.EMPTY;
}
SimpleDateFormat dateFormat = new SimpleDateFormat(SecureCareConstant.WEB_SERVICE_DATE_FORMAT);
return dateFormat.format(new Date(dateUTC.getTime() + TimeZone.getDefault().getOffset(dateUTC.getTime())));
}

KuldeeP ChoudharY
- 446
- 1
- 6
- 22