java - Joda Time - Convert a String into a DateTime with a particular timezone and in a particular format -
i want convert string date datetime object particular timezone , in particular format. how can ?
string date can in format used in world. example mm-dd-yyyy, yyyy-mm-dd, mm/dd/yy , mm/dd/yyyy etc. timezone can legal timezone specified user.
example - convert yyyy-mm-dd mm/dd/yy pacific timezone.
use datetimeformatterbuilder
build formatter able parse/format multiple datetimeformat
s, , set resulting datetimeformatter
use specified datetimezone
:
datetimeparser[] parsers = { datetimeformat.forpattern("mm-dd-yyyy").getparser(), datetimeformat.forpattern("yyyy-mm-dd").getparser(), datetimeformat.forpattern("mm/dd/yyyy").getparser(), datetimeformat.forpattern("yyyy/mm/dd").getparser() }; datetimeformatter formatter = new datetimeformatterbuilder() .append(null, parsers) .toformatter() .withzone(datetimezone.utc); datetime dttm1 = formatter.parsedatetime("01-31-2012"); datetime dttm2 = formatter.parsedatetime("01/31/2012"); datetime dttm3 = formatter.parsedatetime("2012-01-31");
to format given datetime
can use dttm1.tostring("yyyy-mm-dd"))
.
Comments
Post a Comment