java - Joda DateTime,, Formatting, and Mysql Timestamp -
after week of going through many examples, , moving java date, calendar, joda. have decided seek other sources.
the problem:
our table has 2 fields date (timestamp), , tz (string). idea store user's utc in timestamp, , timezone, well, idea. think in utc, , present user time converted timezone on front end (ie, using value store in table.tz)
another requirement use proper object (date, datetime whatever). , not pass string representation of date around. best valid long correctly translated mysql, without having use from_unixtime mysql function in our query.
code using:
public datetime converttimezone(localdatetime date, datetimezone srctz, datetimezone dsttz, locale l) { datetimeformatter formatter = datetimeformat.forpattern("yyyy-mm-dd hh:mm:ss").withlocale(l); datetime srcdatetime = date.todatetime(srctz); datetime dstdatetime = srcdatetime.todatetime(dsttz); system.out.println(formatter.print(dstdatetime)); system.out.println(formatter.parsedatetime(dstdatetime.tostring())); return formatter.parsedatetime(formatter.print(dstdatetime)); }
the string output need (ie utc time, 2013-08-23 18:19:12), formatter.parsedatetime(dstdatetime.tostring()
crashing following error. because of utc timezone independent info, , milleseconds?:
exception in thread "main" java.lang.illegalargumentexception: invalid format: "2013-08- 23t18:19:12.515z" malformed @ "t18:19:12.515z" @ org.joda.time.format.datetimeformatter.parsedatetime(datetimeformatter.java:873) @ com.example.business.rate.ratedeck.converttimezone(ratedeck.java:75) @ com.example.business.rate.ratedeck.writedata(ratedeck.java:143) @ com.example.business.rate.ratedeck.main(ratedeck.java:64)
search engine enriched question:
how format utc joda datetime.
ps first post, , feels nice? :)
thanks in advance,
the new fixed version:
public timestamp converttimezone(localdatetime date, datetimezone srctz, datetimezone dsttz, locale l) { datetime srcdatetime = date.todatetime(srctz); datetime dstdatetime = srcdatetime.todatetime(dsttz); return new timestamp(dstdatetime.getmillis()); }
nick.
it's crashing because format of parsed string doesn't match format of formatter.
the formatter parses using format yyyy-mm-dd hh:mm:ss
, , tostring()
method of datetime
formats date using (as documented) iso8601 format (yyyy-mm-ddthh:mm:ss.ssszz).
Comments
Post a Comment