jodatime - Are time zones lost when passing them via DateTimeFormatter to DateTimeFormatterBuilder? -


i need parse date using couple of date formats assuming particular time zone , output in time zone (i.e. utc). hoping datetimeformatterbuilder appears time zone information attached parsers , printers lost/ignored?

see test , output:

import org.joda.time.datetime; import org.joda.time.datetimezone; import org.joda.time.format.datetimeformat; import org.joda.time.format.datetimeformatter; import org.joda.time.format.datetimeformatterbuilder; import org.joda.time.format.datetimeparser; import org.junit.test;  public class jodabuildertest {   @test public void atestoftimezoneretentionofformatterspassedtobuilder() throws    exception {           string [] parsepatterns = new string [] { "mm/dd/yyyy hh:mm:ss", "mm/dd/yyyy" };      string printpattern = "yyyy-mm-dd't'hh:mm:ss.ssszz";      string inputstring = "08/03/2015 01:01:01";      datetimezone tz = datetimezone.foroffsethours(-3);     datetimeparser[] parsers = new datetimeparser[parsepatterns.length];     (int index = 0; index < parsepatterns.length; index++) {          parsers[index] = datetimeformat.forpattern(parsepatterns[index]).withzone(tz).getparser();     }     datetimeformatterbuilder builder = new datetimeformatterbuilder()       .append(datetimeformat.forpattern(printpattern).withzoneutc().getprinter(), parsers);     datetimeformatter parser = builder.toformatter();      datetimezone parserdtz = parser.getzone();     system.out.println("parserdtz: " + parserdtz);     datetime dt = parser.parsedatetime(inputstring);     system.out.println("parser zone " + dt.getzone());     string dtout = dt.tostring();     system.out.println("expecting utc-3: 2015-08-03t01:01:01.000-03:00");     system.out.println("got:\t\t" + dtout);     string dtoutfmt = dt.tostring(parser);     system.out.println("expecting utc: 2015-08-03t04:01:01.000-00:00");     system.out.println("got:\t\t" + dtoutfmt);     org.junit.assert.assertequals("2015-08-03t04:01:01.000-00:00", dtoutfmt);  } } 

output follows

parserdtz: null parser zone america/new_york expecting utc-3: 2015-08-03t01:01:01.000-03:00 got:        2015-08-03t01:01:01.000-04:00 expecting utc: 2015-08-03t04:01:01.000-00:00 got:        2015-08-03t01:01:01.000-04:00 

so seems ignoring utc-3 set on parsers , utc set on printer , defaulting system zone (edt). missing or expected behavior? recourse not use builder? cannot set zone on parser i.e. datetimeformatter parser = builder.toformatter().withzone(datetimezone.foroffsethours(-3)); because override printer. suppose create separate formatter printer, what's purpose of printer parameter builder? should set null? doesn't feel right.

here clarifications:

datetimeparser , datetimeprinter instances parse/print datetime according formatting pattern. however, time zone , locale not part of printer/parser instance. provided during runtime, on case-by-case basis. see signature of datetimeprinter.printto:

void printto(stringbuffer buf,              long instant,              chronology chrono,              int displayoffset,              datetimezone displayzone, // zone provided @ runtime              locale locale) 

as printer - can read time zone, it's not relevant configure one.

have in mind fragment datetimeprinter/parser javadocs:

internal interface creating/parsing textual representations of datetimes.

so, printer/parser not have timezone set internally. can remove withzone(tz)/withzoneutc() configuration on printers/parsers because don't change there anyway.

however, datetimeformatter has. top-level api should working when comes time zones. if set time zone x on formatter able read time zone string, created datetime instances converted time zone x. likewise, when printing datetime instance (in time zone), printed converted time zone x. if indeed trying parse string -03:00 time zone, need 1 instance of datetimeformatter , when want print in utc, need different formatter instance.

so, having original formatter (renamed parser), can do

datetime dt = formatter.withzone(datetimezone.forid("-03:00")).parsedatetime(inputstring); 

and printing:

string dtoutfmt = dt.tostring(formatter.withzoneutc()); 

i'm sure bit more clear if using relevant names variables - use formatter datetimeformatter instance avoid confusion actual datetimeparsers.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -