Showing posts with label Dates in groovy. Show all posts
Showing posts with label Dates in groovy. Show all posts

Friday, March 19, 2010

Convert Date to String in groovy

import java.text.SimpleDateFormat

long lTime = new Date().time

String formattedDate = getUTCDateString(lTime)
println formattedDate

def getUTCDateString(long lTime)
{
Date lDate = new Date(lTime)
def cal = new GregorianCalendar(TimeZone.getTimeZone('UTC'))
cal.set(lDate[Calendar.YEAR],lDate[Calendar.MONTH],lDate[Calendar.DATE],lDate[Calendar.HOUR_OF_DAY],lDate[Calendar.MINUTE],lDate[Calendar.SECOND])
String formattedDate = new SimpleDateFormat("yyyy-mm-dd hh:mm").format(cal.time);
return formattedDate
}

Sunday, December 6, 2009

Converting String to Date in Groovy

Recently I was looking for a simple way to convert a String object to a Date format in groovy..
below is the example.

In groovy, its just a one liner to make this happen

String timeStampStr = "2009-12-01T12:36:22Z"
def strDate = new Date().parse("yyyy-MM-dd'T'HH:mm:ss'Z'",timeStampStr)
println strDate.