Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy date format for UTC with milliseconds

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.

like image 844
marko Avatar asked Jan 04 '14 16:01

marko


People also ask

What is the format for UTC date?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.


2 Answers

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')) 
like image 76
dhamibirendra Avatar answered Oct 13 '22 20:10

dhamibirendra


I would convert it like that:

def now = new Date() println now.format("YYYYMMdd-HH:mm:ss")  
like image 35
Evilguy Avatar answered Oct 13 '22 20:10

Evilguy