Search

Dark theme | Light theme

August 30, 2010

Groovy Goodness: Convert Date to java.sql.Timestamp

Groovy adds many methods to standard Java classes we can use in our code. To convert a simple Date object to a java.sql.Timestamp we can simply use the toTimestamp() method on a Date object.

import static java.util.Calendar.*

// Create date object with specific year, month and day.
def date = new Date()
date.clearTime()
date.set year: 2010, month: AUGUST, date: 10

// Convert to java.sql.Timestamp.
def sqlTimestamp = date.toTimestamp()
assert 'java.sql.Timestamp' == sqlTimestamp.class.name
assert '2010-08-10 00:00:00.0' == sqlTimestamp.toString()