In this post we will discuss about very important java interview question What are the standard ways for handling TimeZone in database transactions? In this question we will discuss about all the standard practices to be followed for handling TimeZone in database transactions?
- While using PreparedStatement, we should always prefer setDate(int parameterIndex, Date date, Calendar cal) method to specify the Calendar in desired time zone.
- For Spring JDBCTemplate, we should pass Calendar (with desired TimeZone) instance instead of plain Date object.
- We can also set application wide TimeZone using setDefault(TimeZone.getTimeZone(String ID))
- JVM level time zone can be set by passing JVM argument -Duser.timezone=GMT
- Prefer JodaTime API for handling TimeZone specific calculations in your application. JodaTime APIs provides simple and better API’s for playing with Date & TimeZone.
- While persisting time in your application, always prefer to use GMT or any other TimeZone which is not affected by the Day Light Savings. And always include the original timezone name while storing the date so that you can easily re-construct the date to the same value.
- We should never trust Client Date and Time on our server application.
- We should not compare client datetime with server datetime because client machine may have different or incorrect date and time.
You may also like:
What are fail-fast and fail-safe Iterator?
What is difference between Iterator and LisIterator?
Why Prime Numbers are considered in writing certain algorithms like hashcode()?
What are the important paradigms for Developing the Clean Object-Oriented Code?
That’s all about What are the standard ways for handling TimeZone in database transactions?
If you have any feedback or suggestion please feel free to drop in blow comment box.