0

Please help me to print my date in below format Example: 2020-08-05T16:17:10,777 I tried with below date converter but it is not giving the output that I want.

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

 String text = sdf.format(requestTime);
 loggdata.append(REQUEST_TIME + requestDate);

I got date printed like "2020-08-20T06:26:09.003763Z". Date is in UTC tomezone and format is different.

I can see many question and answers here in stackoverflow. But here in my case I need exactly this format 2020-08-05T16:17:10,777 see the last portion ",777". Also I need to display the time in local timezone

Anoop M Nair
  • 1,057
  • 1
  • 13
  • 31
  • 2
    https://stackoverflow.com/questions/3914404/how-to-get-current-moment-in-iso-8601-format-with-date-hour-and-minute – Aayush Mall Aug 20 '20 at 04:31
  • You can use `LocalDateTime.parse(requestTime)` – SSK Aug 20 '20 at 04:50
  • If you used the modern `java.time` classes instead of the outdated `java.util.Date`, it would Just Work. – chrylis -cautiouslyoptimistic- Aug 20 '20 at 05:37
  • 1
    2020-08-05T16:17:10,777 How to generate last part of this example ,I mean ,777 – Anoop M Nair Aug 20 '20 at 06:22
  • It’s not a duplicate of [How to get current moment in ISO 8601 format with date, hour, and minute?](https://stackoverflow.com/questions/3914404/how-to-get-current-moment-in-iso-8601-format-with-date-hour-and-minute) nor of any other question that I could find. It’s special for this question to require comma as decimal separator (which agrees with ISO 8601). – Ole V.V. Aug 20 '20 at 06:38
  • 1
    Replace the `.` with a comma `,`? The newer java time classes would be a better choice, even if it takes a bit longer to figure out. – Joop Eggen Aug 20 '20 at 06:47
  • What's the output of `sdf.format(requestTime)` and what do you want to get? – xingbin Aug 20 '20 at 07:33
  • 1
    You should **drop** `SimpleDateFormat` immediately and start using `DateTimeFormatter`. – MC Emperor Aug 20 '20 at 09:17

1 Answers1

2

Found a solution for the same. I have used "LocalDateTime" for the same.

    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss,SSS");
    String dateInString= now.format(formatter);

It displayed date like this "2020-08-20T21:18:56,321"

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Anoop M Nair
  • 1,057
  • 1
  • 13
  • 31
  • 1
    Thanks for providing your own answer. I applaud switching to java.time, the modern Java date and time API. Two suggestions: (1) Use `ZonedDateTime` rather than `LocalDateTime`. If you’re ever going to use that object for anything else, it’s good that it contains a time zone, and it can never harm. (2) Pass explicit time zone to the `now` method. So `ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));`. The rest is unchanged. – Ole V.V. Aug 21 '20 at 03:24
  • 1
    If you want the time zone of the JVM, pass `ZoneId.systemDefault()`. Then the reader (and you yourself) knows that you have made a conscious choice to use that default time zone. – Ole V.V. Aug 21 '20 at 03:32