Programmers are no stranger to obstacles, but some seem a lot more daunting than others. Time has been my Achilles heel from the beginning–getting time wrong is a major issue for the front end (and the back end too). If a user thinks their Invitation is set for 4:00PM, but they see 9:00PM on their phone, they’ll be wildly confused! So we as developers have to get this right.
In steps JodaTime, a library with the sole intent of making this struggle a little less of a nightmare. It’s still no walk in the park, but it’s definitely an improvement. One of the biggest use cases for time is converting between time zones. For instance, you should store all times in your application in one time zone (with the understanding that you have users from different time zones), and then that time is converted to the user’s time zone on the front end. UTC is the standard that is commonly used for storing timestamps on the back end.
To use JodaTime to convert from UTC to the user’s time, do the following:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZoneUTC(); DateTime dateTime = formatter.parseDateTime(someStringTime.withZone(DateTimeZone.getDefault();
Of course, there’s more than one way to skin a cat! Head over to JodaTime’s site, and figure out a way that works for you!
Brandon from The Bunch