How to format now()?

rat hole alert! that sign goes off all the time now trying to learn this language... so... can someone please show me, in two lines, how to take "now()", which I know is epoch, and return a string formatted as "Wed Jan 17 19:33:40", without using toString, because I have a workaround using that value. Thanks!

Can you just use

new Date()

If you need the date in that specific format you will need to also use SimpleDateFormat. You might find this article useful:

1 Like

This should be close

import java.text.SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat(“EEE MMM dd HH:mm:ss”)
dateString = sdf.format(now())

I've got this in my Utilities Library:

String nowFormatted() {
  if(location.timeZone) return new Date().format('yyyy-MMM-dd h:mm:ss a', location.timeZone)
  else                  return new Date().format('yyyy-MMM-dd h:mm:ss a')

Date.format() takes formatter parameter for a date that are the same as probably 99% of languages currently in use. As @thebearmay said, "EEE MMM dd HH:mm:ss" would what you want.

I recommend putting it into a library that you can use throughout your apps and drivers, so you can easily re-use it.


Thank you all! I was really close, just missing the parens. Its this goofy syntax and all these different methods and classes that makes it difficult for a newbie. I try stuff from the good doc I find online only to find out it doesn't work because i don't have the class defined and then i go to import it and its not available. So whats a newbie to do? Ask guys like you! Thanks again...


When in doubt, put parentheses around it :rofl: :rofl:

(I hate groovy. But it's better than having to "self." reference every function with Python!)


You can do what I do... @CompileStatic basically everything, strict type everything, and treat Groovy like a bastardized version of Java.