如何以ISO8601格式打印日期字符串?

huangapple go评论102阅读模式
英文:

How to print date string with ISO8601 format?

问题

请帮我按照下面的格式打印日期:
示例:2020-08-05T16:17:10,777
我尝试了下面的日期转换器,但它没有给出我想要的输出。

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

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

我得到的日期打印格式为:"2020-08-20T06:26:09.003763Z"。日期在UTC时间区,格式不同。

我在stackoverflow上看到了很多问题和答案。但在我的情况下,我需要的正好是这个格式:2020-08-05T16:17:10,777,请注意最后的部分",777"。
并且我需要以本地时区显示时间。

英文:

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

答案1

得分: 2

找到了同样的解决方案。我已经使用了“LocalDateTime”来实现相同的功能。

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

它显示的日期是这样的:“2020-08-20T21:18:56,321”

英文:

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"

huangapple
  • 本文由 发表于 2020年8月20日 12:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63498179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定