Groovy date format for UTC with milliseconds

前端 未结 3 955
后悔当初
后悔当初 2021-01-07 17:35

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

I wasn\'t able

相关标签:
3条回答
  • 2021-01-07 17:59

    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'))
    
    0 讨论(0)
  • 2021-01-07 18:07

    I would convert it like that:

    def now = new Date()
    println now.format("YYYYMMdd-HH:mm:ss") 
    
    0 讨论(0)
  • 2021-01-07 18:22

    You can try this:

    TimeZone.getTimeZone('UTC')
    Date date = new Date()
    String newdate = date.format("YYYY-MM-dd HH:mm:ss.Ms")
    log.info newdate
    
    0 讨论(0)
提交回复
热议问题