Anoop M Nair
Anoop M Nair

Reputation: 1087

How to print date string with ISO8601 format?

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

Upvotes: 0

Views: 1181

Answers (1)

Anoop M Nair
Anoop M Nair

Reputation: 1087

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"

Upvotes: 2

Related Questions