Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring @DateTimeFormat conversion error with Daylight Saving Time
    primarykey
    data
    text
    <p>I'm using Spring MVC on a machine that has Daylight Saving Time configured (America/Sao_Paulo Time Zone). In my form class I used the annotation DateTimeFormat to configure the output of my Date:</p> <pre><code>public class JustificativaOcorForm { ... @NotNull @DateTimeFormat(pattern="yyyy-MM-dd") private Date dataMarcacao; ... } </code></pre> <p>While debugging I'm getting the date 16/10/2011 (dd/MM/yyyy) that's the start of the daylight time, but Spring converts it to 2011-10-15. Why?</p> <pre><code>2011-11-04 16:35:31,965 [http-8080-Processor25] DEBUG org.springframework.core.convert.support.GenericConversionService - Converting value Sun Oct 16 00:00:00 BRST 2011 of [TypeDescriptor @javax.validation.constraints.NotNull @org.springframework.format.annotation.DateTimeFormat java.util.Date] to [TypeDescriptor java.lang.Long] 2011-11-04 16:35:31,965 [http-8080-Processor25] DEBUG org.springframework.core.convert.support.GenericConversionService - Converted to 1318730400000 2011-11-04 16:35:32,010 [http-8080-Processor25] DEBUG org.springframework.core.convert.support.GenericConversionService - Converted to '2011-10-15' </code></pre> <p>I see this question: <a href="https://stackoverflow.com/questions/7817898/datetimeformat-in-spring-produces-off-by-one-day-error">@DateTimeFormat in Spring produces off-by-one day error</a></p> <p>but Spring 3 uses Joda-Time and I have joda-time-2.0.jar in my classpath so I don't know why this occurs and how I can solve it.</p> <p><strong>[EDIT]</strong></p> <p>I've tested creating LocalData objets, and found something:</p> <pre><code>LocalDate ld = new LocalDate( new SimpleDateFormat("dd/MM/yyyy").parse("16/10/2011").getTime() ); System.out.println( new SimpleDateFormat("dd/MM/yyyy HH:mm:ss Z z").format( ld.toDate() ) ); //prints 15/10/2011 00:00:00 -0200 BRST LocalDate ld2 = new LocalDate( 2011,10,16 ); System.out.println( new SimpleDateFormat("dd/MM/yyyy HH:mm:ss Z z").format( ld2.toDate() ) ); //prints 16/10/2011 00:00:00 -0200 BRST </code></pre> <p>It seems that the first approach is thinking that the time is in UTC, because debugging I can see that Joda use the method convertUTCToLocal of the class DateTimeZone.</p> <p>Maybe this is the default of the Spring too, he expect's one date in UTC too and i am passing the BRT Date.</p> <p>So I think my solution is change the objects to LocalDate and use the second way to create the instance of this object.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload