sree
sree

Reputation: 902

DateTimeFormat annotation in spring time zone issue

I am having an issue to convert string to date object. While converting it is picking the GMT timezone and getting changed into previous date. I want to ignore the timezone.If the string value is "03/12/2019" then it should the same date irrespective of the timezone.

@DateTimeFormat(pattern = "MM/dd/yyyy") Date startDate

Any Suggestions? Thanks.

Upvotes: 1

Views: 2880

Answers (3)

Md. Sajedul Karim
Md. Sajedul Karim

Reputation: 7065

You can solved it by using Jodatime LocalDate (which is without time zone by design):

@DateTimeFormat(iso = ISO.DATE) LocalDate startDate

It can be converted to JDK Date by calling toDate() method.

You can also use for date pattern

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE, pattern = "MM/dd/yyyy") 

Dependency for maven:

<dependency>
   <groupId>joda-time</groupId>
   <artifactId>joda-time</artifactId>
   <version>2.9.3</version>
</dependency>

Upvotes: 2

DCO
DCO

Reputation: 1292

Use java.time.LocalDate LocalDate has no Time and therefore has no Timezone

@DateTimeFormat(pattern = "MM/dd/yyyy")
private LocalDate localDate;

Upvotes: 1

codiallo
codiallo

Reputation: 183

USe java.time.LocalDate instead of Date.

Upvotes: 1

Related Questions