You'll need to use the UTC zone when parsing:

formatter.withZone(DateTimeZone.UTC).parse();

I know its a pain. JSR-310 will be better.

Stephen


2009/3/6 Michel Tremblay <santar...@gmail.com>:
> I came across a strange behaviour in the DateTimeFormatter`s parsing
> methods.   Depending on the time zone the date will either parse file or
> generate a illegal instant through a IllegalArgumentException.  Now frrom
> what I understand this means that the date, through manipulations ends up
> being impossible (like 2:30 the morning where the time changes in US)
> However, the date I ask to be parsed does not have time information nor does
> the pattern, so I would not expect that I get impossible date-time like
> this.
>
> Here is an example that explains it :
>
> If I run this code in Canada (set time to EST) all is good and I get the
> correct answer.
>
> public class App
> {
>     public static void main(String[] args)
>     {
>         System.out.println("Hello World!");
>         ReadableInstant ins = null;
>         DateTimeFormatter formatter = null;
>         formatter = DateTimeFormat.forPattern("yyyyMMdd");
>         formatter =
> formatter.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("EST")));
>
>         ins = formatter.parseDateTime("19880410");
>
>         System.out.println(formatter.print(ins));
>     }
> }
>
> result is as expected :
>
> Hello World!
> 19880410
>
> however... If I move to Asia time (Asia/Shanghai) the situation changes
> completely :
>
> public class App
> {
>     public static void main(String[] args)
>     {
>         System.out.println("Hello World!");
>         ReadableInstant ins = null;
>         DateTimeFormatter formatter = null;
>         formatter = DateTimeFormat.forPattern("yyyyMMdd");
>
>         formatter =
> formatter.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone("Asia/Shanghai")));
>
>         ins = formatter.parseDateTime("19880410");
>
>         System.out.println(formatter.print(ins));
>     }
> }
>
> and the result now becomes
>
> Hello World!
> Exception in thread "main" java.lang.IllegalArgumentException: Cannot parse
> "19880410": Illegal instant due to time zone offset transition
> (Asia/Shanghai)
>     at
> org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:336)
>     at
> org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:662)
>     at foo.App.main(App.java:28)
>
> Notice that only the timezone has changed.  This is a bit problematic as the
> class where it is used cannot be given a set timezone and we relying on the
> sytem OS to provide the information.  I could force a timezone but that
> would defeat the purpose.
>
> I frankly do not see how someone`s birthday could become an impossible time
> just by changing the timezone from which you read the information.  also, it
> is somewhat uncommon that someone would list the exact time with TZ of his
> birthday !! :-)
>
> This is why I think I am looking at a bug somewhere in the stack of
> JodaTime, could someone confirm this behaviour and help me find a solution ?
>
> great many thanks.
>
> Santaroga
>
>
> ------------------------------------------------------------------------------
> Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
> -OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
> -Strategies to boost innovation and cut costs with open source participation
> -Receive a $600 discount off the registration fee with the source code: SFAD
> http://p.sf.net/sfu/XcvMzF8H
> _______________________________________________
> Joda-interest mailing list
> Joda-interest@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
>

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Joda-interest mailing list
Joda-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/joda-interest

Reply via email to