Forum Discussion

N78A's avatar
N78A
Contributor
6 years ago

Converting value of timestamp to expected format - groovy Soapui

I am trying to convert value of timestamp fetched from hive database to an expected format. Can some one please help !

 

Timestamp fetched :

 

TRANSACTION_TIMESTAMP>20170210141809</TRANSACTION_TIMESTAMP

 

Required format:

 

2017-02-10 14:18:

 

Code to fetch from xml for further operation:

 

String timeStamp1 = results1[i].get("transactionTimeStamp")

 

// 'results1[i].get' is written to bring all the timestamps from xml as there are multiple instances of timestamp in the xml.

 

Now I found below but dont know how to use this in my code to first convert fetched value to date & then convert to desired format

 

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

 

8 Replies

  • Lucian's avatar
    Lucian
    Community Hero

    Hey,

     

    This is a piece of code which should work:

     

    import java.text.SimpleDateFormat
    
    // Get timestamp
    def timestamp = 1526199928404
    // Set a date using the timestamp
    def date = new Date( timestamp )
    
    // Create a date format as desired
    SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm" )
    // Display your date in the defined format
    log.info dateFormat.format( date )

    Cheers!

    • N78A's avatar
      N78A
      Contributor

      Hi ,

       

      Thanks for your response. Tried the option you suggested but it gives me error:

       

      ""Illlegal argument for below line ,

      def date = new Date( timestamp )

       

  • JHunt's avatar
    JHunt
    Community Hero
    String s = '20170210141809'
    Date myDate = Date.parse('yyyyMMddHHmmss', s)
    assert myDate.format('yyyy-MM-dd HH:mm:') == '2017-02-10 14:18:'

     

    • N78A's avatar
      N78A
      Contributor
      Hi, Thanks for your response. I tried the method you suggested, but it gives me error : "unparseable date"
      • JHunt's avatar
        JHunt
        Community Hero

        My script didn't work?

         

        I presumed you made a typo when you said that you fetched the timestamp in this format:

         

        TRANSACTION_TIMESTAMP>20170210141809</TRANSACTION_TIMESTAMP

        So I was working on the basis of just starting from:

        20170210141809

        If you had to start from the top one, you could just do this to get to the second one:

         

        String s = 'TRANSACTION_TIMESTAMP>20170210141809</TRANSACTION_TIMESTAMP'
        Date myDate = Date.parse('yyyyMMddHHmmss', s[22..35])
        assert myDate.format('yyyy-MM-dd HH:mm:') == '2017-02-10 14:18:'

        That would get it working, but in that case perhaps it would be better to work on the way you are getting hold of the dates? Perhaps you could post that part of your code.

         

  • Lucian's avatar
    Lucian
    Community Hero

    If your timestamp is a string you can use:

     

    def date = new Date( timestamp as long )

    This will convert it to a long integer.

    • N78A's avatar
      N78A
      Contributor

      Hi,

       

      Thanks again.

      Sorry I couldnt try again the solution suggested. Will check and come back