I need to store date/time in UTC zone into MySQL database (of a DATETIME type column). When a user enters a date, it is first converted to org.joda.time.DateTime
Your concrete problem is caused because DateTime#toDate() doesn't use the time zone during the conversion to java.util.Date
. It basically returns new Date(millis)
wherein the millis
is the internally stored epoch time of DateTime
instance, exactly as described by DateTime javadoc and required by the java.util.Date constructor.
In other words, the withZone(DateTimeZone.UTC)
part has totally no effect here. The code behaves exactly the same as if that part was absent. That explains why you end up with the originally entered time.
Technically, the problem is in your custom JSF converter which converts from String
to DateTime
. That converter is apparently not taking the time zone into account and assuming that the input is already in GMT timezone (as by default). The converter must be instructed that the input is in IST timezone. If you were using standard java.util.Date
property with standard JSF <f:convertDateTime>
, then you could have solved it by setting its timeZone
attribute to IST
.
<h:inputText value="#{bean.date}">
<f:convertDateTime pattern="dd-MMM-yyyy hh:mm:ss a" locale="en" timeZone="IST" />
</h:inputText>
Your custom JSF converter should under the covers be doing exactly the same: tell the API that the supplied String
is in IST timezone instead of letting it assume that it's already in GMT timezone. You didn't show the custom JSF converter anywhere, so it's hard to supply the exact answer, but it should boil down to the following kickoff example:
String inputDateString = "02-Oct-2013 11:34:26 AM";
String inputDatePattern = "dd-MMM-yyyy hh:mm:ss a";
TimeZone inputTimeZone = TimeZone.getTimeZone("IST");
DateTime dateTime = DateTimeFormat
.forPattern(inputDatePattern)
.withZone(DateTimeZone.forTimeZone(inputTimeZone))
.parseDateTime(inputDateString);
The resulting DateTime
instance will end up having the right internal epoch time in millis.