简体   繁体   中英

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM