Forum Discussion

taewoo's avatar
taewoo
New Contributor
15 years ago

Groovy: How to convert UTC time into integer?

I have a timestamp in this format: 2009-09-10T15:00:00Z

how do I convert this to integer (seconds since 1970)?

3 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello,

    You have to parse your time stamp string to that, check : http://groovycookbook.org/basic_types/dates_times/ you get how to for this. In general your script could look like this:


    import java.text.*
    import java.util.*

    def cal = Calendar.instance

    def timeS = "2009-09-10 15:00:00"
    Date d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(timeS)
    cal.setTime d1
    println cal.getTimeInMillis()



    let me know does it helps,
    robert
  • taewoo's avatar
    taewoo
    New Contributor
    Thanks Robert.
    does your code sample take into account the letters "T" and "Z" in the time string?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hello,

    No. You should write your own date format:

    new SimpleDateFormat("Date Format")


    robert