Reputation: 611
I have an array or times/values coming back to be in an array like:
[0, 60]
Which are times in minutes, 0 = 12:00 a.m, 60 = 1:00 a.m. I am wanting to store these in an oracle database as timestamps. How do I convert minutes into timestamps in groovy?
EDIT:* The date can be arbitrary, I just need the time portion for my purposes. EDIT:* For the above example I would need two timestamps maybe similar to this:
1/1/1970 12:00:00.000000 AM // for 0
1/1/1970 1:00:00.000000 AM // for 60
EDIT:
I can get the following so far:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
def date = new Date(0)
gives me:
Thu Jan 01 00:00:00 UTC 1970
So now How do I set the time based off of a value in minutes?
Upvotes: 0
Views: 591
Reputation: 66069
I assume you want to use the current day offset with the number of minutes given for your timestamp.
Since a new Date or Timestamp will be initialized to the current time and date, you can use that and override the minute field with the values from your array. Values 60 and greater will automatically carry over into hours, days, and so forth. Here's one way to do it:
def times = [0, 60]
def dates = times.collect { new Date().clearTime().copyWith(minute: it) }
def timestamps = dates.collect { new java.sql.Timestamp(it.time) }
println timestamps
// output: [2015-06-23 00:00:00.0, 2015-06-23 01:00:00.0]
The copyWith
method was added in groovy 2.2.0. In older versions, you can use something like this:
def d = new Date().clearTime()
d.minutes = 60
Upvotes: 1