I wanted to store current date time (in UTC time zone) in ISO format in Mongo with Groovy as the language. It is pretty straightforward with joda time library. First the code and then some explaination.

import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormatter
import org.joda.time.format.ISODateTimeFormat

import java.text.SimpleDateFormat

/**
 * Created by ravi on 7/31/14.
 */
class DateTimeHelper {
    def static getDateTimeInUtc(Date date, String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") {
        SimpleDateFormat dateFormatUtc = new SimpleDateFormat(dateFormat)
        dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"))
        return dateFormatUtc.format(date)
    }


    def static Date getCurrentUTCDateTimeInISOFormat() {
        def currentDateTimeInUtc = DateTimeHelper.getDateTimeInUtc(new Date())
        DateTimeFormatter parser = ISODateTimeFormat.dateTime()
        DateTime parsedDateTime = parser.parseDateTime(currentDateTimeInUtc)
        return parsedDateTime.toDate()
    }
}

First step is to get current date time in UTC format:
def currentDateTimeInUtc = DateTimeHelper.getDateTimeInUtc(new Date())

Create ISODateTimeFormatter:
DateTimeFormatter parser = ISODateTimeFormat.dateTime()

Parse UTC date time string using ISODateTimeFormatter:
DateTime parsedDateTime = parser.parseDateTime(currentDateTimeInUtc)

Return java's Date object via parsedDateTime.toDate() method:
parsedDateTime.toDate()

Done. The getDateTimeInUtc method is just used to convert date to the UTC time zone value and format it using the passed in format string.

Thoughts? Could it be shorter/sweeter? Please let me know your thoughts in the comments section. Thank you for sharing your valuable time!