Groovy date format for UTC with milliseconds

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

I'm having trouble finding a good way of formatting a UTC-time stamp with this format: yyyyMMdd-HH:mm:ss.<three additional digits>

I wasn't able to find any character that represents milliseconds/hundredths, I'm not even sure this is possible, to parse that format that is.

Ideally I'd like to use the parseToStringDate that's part of the Date library.

My plan b is to convert yyyyMMdd-HH:mm:ss to milliseconds and then add the three last digits to that number.

回答1:

Use yyyyMMdd-HH:mm:ss.SSS

This will get you milliseconds as well.

Test Code:

def now = new Date() println now.format("yyyyMMdd-HH:mm:ss.SSS", TimeZone.getTimeZone('UTC')) 


回答2:

TimeZone.getTimeZone('UTC') Date date= new Date() String newdate=date.format("YYYY-MM-DD HH:mm:ss.Ms") log.info newdate 


回答3:

I would convert it like that:

def now = new Date() println now.format("YYYYMMdd-HH:mm:ss")  


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!