Forum Discussion

swprabhu's avatar
swprabhu
Occasional Contributor
7 years ago

how to format a date

My input timestamp is in the format "yyyy-mm-dd hh:mm:ss.6additional digits". I need to convert this into the format 

"yyyy-mm-dd-hh.mm.ss.6additionaldigits".  This has to be done through groovyscripting.

 

eg : I ran the below code but get an error

def soaprep = "2014-02-11 22:57:12.650224"

def soapform= new Date().parse("yyyy-mm-dd hh:mm:ss.SSSSSS",soaprep)

log.info soapform

 

def formattime = soapform.format("yyyy-mm-dd-hh.mm.ss.SSSSSS")

log.info formattime

 

Results (which is not what I expect)

soapform =Sat jan 11 23:08:02 CST 2014

formattime = 2014-08-11-23:08:02.000224

 

My expected result is  2014-02-11-22.57.12.650224

9 Replies

  • Lucian's avatar
    Lucian
    Community Hero

    Hi,

     

    This is a piece of code which seems to do what you want:

     

    import java.text.SimpleDateFormat
    
    // This was your String
    def soaprep = "2014-02-11 22:57:12.650224"
    
    // Define the format for the date
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd hh:mm:ss.SSSSSS" )
    
    // Convert to date
    Date yourDate = sdf.parse( soaprep )
    // Display with the desired format
    sdf.format(yourDate)

    But I am curious why would you need something like this.

  • swprabhu's avatar
    swprabhu
    Occasional Contributor

     

     

    The expected format is the output from another application which I need to compare with . Hence require my input to be in that format

  • swprabhu's avatar
    swprabhu
    Occasional Contributor

    The above code doesnot give me the correct result. It returns the time incorrectly. I want the same values in the response just with additional hyphen and semicolon changed to a dot.

    • Lucian's avatar
      Lucian
      Community Hero
      import java.text.SimpleDateFormat
      
      // This was your String
      def soaprep = "2014-02-11 22:57:12.650224"
      
      // Define the format for the date
      SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd hh:mm:ss.SSSSSS" )
      
      // Convert to date
      Date yourDate = sdf.parse( soaprep )
      
      // Define a new format
      SimpleDateFormat newFormat = new SimpleDateFormat( "yyyy-mm-dd-hh.mm.ss.SSSSSS" )
      
      // Display with the desired format
      newFormat.format(yourDate)

      I don't really understand the use case for this but it doesn't look like the right way to do it. Anyways, the above code should do it.

      • swprabhu's avatar
        swprabhu
        Occasional Contributor

        You may be right. May be this is not the right way. This timstamp has additional 6 digits which I donot know what they represent. Hence even with the code you gave I get an incorrect value for the time field . 

         

        Instead of the  expected result    2014-02-11-22.57.12.650224, I get the value as 2014-02-11-11.08.02.000224

         

         

        Is there anyway I can convert the string 2014-02-11 22:57:12.650224   to the format 2014-02-11-22.57.12.650224. 

  • CharlesHarold's avatar
    CharlesHarold
    Occasional Contributor

    Yes, the last six numbers are milliseconds. With a DB2 database, the output format for a timestamp (when queried) is "2014-02-11 22:57:12.650224", but if you need to set a timestamp, you have to format it "2014-02-11-22.57.12.650224".

     

    Since this is just a string, try:

     

    def ts = "2014-02-11 22:57:12.650224"
    
    // Replace space with dash ts = ts.replaceAll( " ", "-" ) // Replace colons with periods ts = ts.replaceAll( ":", "." ) log.info ts