Ashok Kumar N
Ashok Kumar N

Reputation: 573

How to Fetch records in hibernate with specified Timezone?

  1. I need to give support for different timezone for my application.
  2. 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.
  3. Each request is coming from the different timezone. based on the timezone I need to parse the UTC time to that specific timezone.
  4. 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).

Upvotes: 1

Views: 1810

Answers (2)

KuldeeP ChoudharY
KuldeeP ChoudharY

Reputation: 428

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())));
}

Upvotes: 0

Florian Bronder
Florian Bronder

Reputation: 123

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.

Upvotes: 3

Related Questions