英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论