• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Current date (mm/dd/yyyy)

 
ted bishop
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get the current date in format mm/dd/yyyy. Using the code below I'm getting the date with a 2 digit year and no leading zeros (4/1/11).

<groovy>
def dateTime = new Date()
def date = dateTime.getDateString()
step.setWebtestProperty('send.date', date)
</groovy>

Thanks,
Ted
 
Brian Mooney
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ted bishop wrote:I'm trying to get the current date in format mm/dd/yyyy. Using the code below I'm getting the date with a 2 digit year and no leading zeros (4/1/11).

<groovy>
def dateTime = new Date()
def date = dateTime.getDateString()
step.setWebtestProperty('send.date', date)
</groovy>

Thanks,
Ted


Hey,

You should be able to use the SimpleDateFormat class from java. Not sure if there's a more Groovylicious way to do things but there's not much code involved in that.
You can read up about the SimpleDateFormat class though its API documentation SimpleDateFormat

Should be as simple as defining you're required date format and then formating the Date object to return the required string.

Amn't familiar with the setWebTestProperty method from the step class?
But the below seems to run ok from the groovy cmd line groovysh.


Prints out as:

04/06/2011
 
ted bishop
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,

Thanks for your reply. I got it to work using your example.

Thanks,
Ted
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Mooney wrote: Not sure if there's a more Groovylicious way to do things but there's not much code involved in that.

Actually yes. Groovy provides a wrapper to SimpleDateFormat. It's only one line less than your Java version though.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic