Date & Joda DateTime
Its always been a challenge for me when it comes to "Dates" be it java or real life. I had a task to make all the dates in my project UTC dates. For newbeeis "UTC" date means the date should reflect date that would reflect the time at GMT at any given time. Which means you need to +/- the hours based on the offset from the GMT.
A quick and easy way to do this is using Joda DateTime. I used it and here is a sample code to illustrate how it can be implemented:
Its also possible to convert a local time to another timezone using this feature. For example from IST to EST using the same code by adjusting the timezone constant.
However the challenge i faced was, I realized that my database (Oracle) used datatype "date" which has seamless integration with java.lang.Date. Converting a joda DateTime with timezone offset to java Date is a bit tricky. I found a DateFormat that suited my case: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX".
Unfortunately this date format only works on Java7 and I was supposed to be Java6 compliant. After a lot for struggle looking for suggestions on forums I finally pulled out a old trick out of my new hat ;)
The code that worked for this conversion in my case was:
A quick and easy way to do this is using Joda DateTime. I used it and here is a sample code to illustrate how it can be implemented:
Its also possible to convert a local time to another timezone using this feature. For example from IST to EST using the same code by adjusting the timezone constant.
However the challenge i faced was, I realized that my database (Oracle) used datatype "date" which has seamless integration with java.lang.Date. Converting a joda DateTime with timezone offset to java Date is a bit tricky. I found a DateFormat that suited my case: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX".
Unfortunately this date format only works on Java7 and I was supposed to be Java6 compliant. After a lot for struggle looking for suggestions on forums I finally pulled out a old trick out of my new hat ;)
The code that worked for this conversion in my case was:
jodaDate.getYear() did not work for me as it returned extremely future date, centuries ahead of actual date. I actually used my business logic to compute the year.
Comments
Post a Comment