0

I'm working on Spring boot project and I want to convert a String date coming from a post request

D,100000001028686,BA0884,72087000000176,N,2,147568593,DEPOSITREFERENCE,2020-08-05 20:17:33.32691, 601123,ZAR,2500,57,24,i10c=0,i20c=2,i50c=5,iR1=2,iR2=5,iR5=8,iR10=200,iR20=1,iR50=55,iR100=60,iR200=82,0,0,0,0,000

The date that I want to convert is in Bold and need to convert that part from a @PostMapping method request parameter into one of the java.time Objects.

After searching I found some solution for the data if self without using Spring but it did not work for me and used java.util.Date, here the code I wrote so far

    class Main {
      public static void main(String[] args) throws ParseException {
        String date = "2020-08-05 20:18:33.32692";
        System.out.println(covertDate(date)); //Wed Aug 05 20:19:05 UTC 2020
      }
    
       public static Date covertDate(String date) throws ParseException {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSS");
            return formatter.parse(date);
        }
    }

The response I got is not what I'm looking for, is there any way to solve the problem

treedust
  • 137
  • 2
  • 13
Farhat Amine
  • 51
  • 1
  • 7
  • 4
    Do not longer use SimpleDateFormat and java.util.Date. Use the modern java.time API available since java 8 – Jens Aug 06 '20 at 18:23
  • @Jens , Plz can u clarify like just put me on the right direction and I will take from there – Farhat Amine Aug 06 '20 at 18:25
  • Please, when asking for help with code that doesn’t work, be precise about expected result and how observed result differs. Paste any error message into the question. – Ole V.V. Aug 09 '20 at 09:05
  • Does this answer your question? [Timestamp convert \[duplicate\]](https://stackoverflow.com/questions/58705924/timestamp-convert) – Ole V.V. Aug 09 '20 at 09:08

2 Answers2

2

Here the solution I found after searching for future I used Java 8 API to solve it

 class Main {
  public static void main(String[] args) throws ParseException {
    String sDate6 = "2020-08-05 11:50:55.555";
    System.out.println(covertDate(sDate6)); //2020-08-05T11:50:55.555
  }

   public static LocalDateTime covertDate(String date) throws ParseException {
         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);
         LocalDateTime dateTime = LocalDateTime.parse(date,formatter);
         return dateTime;  
    }
}
Farhat Amine
  • 51
  • 1
  • 7
1

In JShell (Java Version 14) I ran your code and was able to get the similar results (Granted it is in GMT and not UTC; however, the seconds are offset by the same amount as your current output): enter image description here

If the question is about UTC:

I would suggest to use Instant as it avoids many of the issues that LocalDateTime has presented over the years. As mentioned in the comments is it generally best to avoid using java.util.Date and to use Instant instead (or you could use the more traditional LocalDateTime).

If you are talking about Spring's annotated @PostMapping method to parse out the date automatically you could use something like:

@PostMapping
public String postDate(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Long dateReq) {
  Instant date = Instant.ofEpochMilli(dateReq);
  System.out.println(date);
}

If you wanted to use your custom formatter the you could do @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSS" LocalDateTime date) as the parameter of the postDate method.

Please note that Spring's docs state that the pattern field of @DateTimeFormat uses the same patterns as java.text.SimpleDateFormat. So that could be of use.

treedust
  • 137
  • 2
  • 13