0

I am using Spring annotation while passing datetime as string from frontend to validate that date pattern and convert that string to datetime.I want to generate a joda datetime object of local timezone for a date as in the code given below:

@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
@JsonDeserialize( using = DateTimeDeSerializer.class )
    private DateTime lastModifiedDate;

DateTimeDeSerializer.java

 DateFormatter dateFormatter = new DateFormatter("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");

    Locale locale=new Locale("en");

    String date = "2016-04-19T17:22:29.296+05:30";

    DateTime localDatime = new DateTime(dateFormatter.parse(date , locale));

I am getting "Unparseable date: "2016-04-19T17:22:29.296+05:30".Any suggestions for solving this will be of great help.

JavaUser
  • 33
  • 9
  • Where is the `'GMT'` in your String to parse? Please read the linked question. – Tunaki Apr 20 '16 at 11:03
  • @Tunaki I have added 'GMT' in datestring(2016-04-19T17:22:29.296GMT+05:30 )but I still got the same exception.The link which you have posted didn't solve or provide a solution to my problem and searched other links but couldn't find any solution;hence it can't be marked as duplicate. – JavaUser Apr 20 '16 at 11:10
  • Read it carefully, it explains how to parse a String into a Date, with all the possibilties. – Tunaki Apr 20 '16 at 11:11
  • @Tunaki I have already read that but I want to convert my datestring containing timezone also to joda datetime which is not explained using DateFormatter API of Spring Framework.It explains only on how to parse a string which contains date and time to Date object. – JavaUser Apr 20 '16 at 11:19
  • @Tunaki I don't understand why you have marked it as duplicate and later downvoted it when the link clearly didn't solve my problem.The solution which you have provided is clearly doing the reverse way.It is taking January 10 .. as input string and outputs 2010-01-02T00:00:00.000-10:00 but my requirement is just reverse I have to take 2010-01-02T00:00:00.000-10:00 as input and parse that to datetime.Please don't downvote or duplicate without proper explanation. – JavaUser Apr 20 '16 at 11:33
  • I have given more explanation that you'd need. Your code _cannot work_ because _you are not parsing the String correctly_. To parse the String correctly, _read the linked question_. – Tunaki Apr 20 '16 at 11:34
  • Sorry for the previous comments.If I use yyyy-MM-dd'T'HH:mm:ss.SSSXXX as pattern in DateFormatter it's working but if I use that pattern in @DateTimeFormat Spring annotation its throwing exception.Will edit this question. – JavaUser Apr 20 '16 at 12:00
  • The below code is working well : String dateSTring = "2016-04-19T17:22:29.266+05:30"; DateFormatter dateFormatter = new DateFormatter("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); Locale locale=new Locale("en"); try { System.out.println(new DateTime(dateFormatter.parse(dateSTring, locale))); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } – JavaUser Apr 20 '16 at 13:00

0 Answers0