On 13 January 2012 18:13, Elliot Metsger <emets...@gmail.com> wrote:
>    @Test
>    public void testDateTimeEquality() throws Exception {
>        final DateTime now = DateTime.now().toDateTimeISO();
>        final DateTime parsed = DateTime.parse(now.toDateTimeISO().toString());
>        assertEquals(now.getZone(), parsed.getZone());
>        assertEquals(now, parsed);
>    }
>
> It fails with:
> java.lang.AssertionError:
> Expected :America/New_York
> Actual   :-05:00
>
> I've tried this test a few different ways and I am not sure how, or if
> it is possible, to get the parsed DateTime to be equal to the instance
> produced by DateTime.now().  Any ideas?  I'm using JodaTime 2.0.

Parsing in Joda-Time has awkward corner cases around offsets and
zones. The parse() method was meant to help but its behaviour is also
a bit weird in some ways (and not well docced). The parse method
parses any offset (such as +02:00) in the text and creates a time-zone
fixed at that offset.

So, the options are:
DateTime.parse(string) - parses the offset and creates a fixed offset zone
DateTime.parse(string).withZone(null) - parses and then converts to
the default zone (or any other you specify)
new DateTime(string) - parses to the default zone

Thus, #2 and #3 above probably do what you want.

I'm not sure that I can alter the parse method without breaking
someone else, nor am I 100% sure I'd want to change its behaviour.

Stephen

------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
Joda-interest mailing list
Joda-interest@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/joda-interest

Reply via email to