16

I am working with groovy (gremlin to traverse a graph database to be exact). Unfortunately, because I am using gremlin, I cannot import new classes.

I have some date values that I wish to convert to a Unix timestamp. They are stored as UTC in the format: 2012-11-13 14:00:00:000

I am parsing it using this snippet (in groovy):

def newdate = new Date().parse("yyyy-M-d H:m:s:S", '2012-11-13 14:00:00:000')

The problem is that it does a timezone conversion, which results in:

Tue Nov 13 14:00:00 EST 2012

And if I then convert that to a timestamp using time(), that gets converted to UTC, then the timestamp generated.

How do I get new Date() to not do any timezone conversions when the date is first parsed (and just assume the date as UTC)?

F21
  • 32,163
  • 26
  • 99
  • 170
  • well, `new Date().parse("yyyy-M-d H:m:s:S" + " Z", '2012-11-13 14:00:00:000' + ' 0000')` comes to mind... – vladr Nov 15 '12 at 02:26
  • @vladr: The groovy console just throws an error at me and says it's unparseable :( `java.text.ParseException: Unparseable date: "2012-11-13 14:00:00:000 0000"` – F21 Nov 15 '12 at 02:29
  • 1
    @F21 @vladr: That solution works for me if you change it to `def newdate = new Date().parse("yyyy-M-d H:m:s:S Z", '2012-11-13 14:00:00:000' + ' UTC')`. `#parse` is static btw, so unless Gremlin forces you to you shouldn't need to create an instance of `Date` to call it. – Justin Piper Nov 15 '12 at 03:26
  • @JustinPiper Gremlins made me do it, I swear! :) (copy-paste that is) | @F21, I'm sorry, the timezone should have been `' +0000'`, not `' 0000'` | @jahroy, I thought the example I gave appended the timezone, don't you? :) – vladr Nov 15 '12 at 03:52
  • @vladr - My apologies... I overlooked it (hard to read code in comments sometimes). – jahroy Nov 15 '12 at 04:02

3 Answers3

20

Here are two ways to do it in Java:

/*
 *  Add the TimeZone info to the end of the date:
 */

String dateString = "2012-11-13 14:00:00:000";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d H:m:s:S Z");
Date theDate = sdf.parse(dateString + " UTC");

/*
 *  Use SimpleDateFormat.setTimeZone()
 */

String dateString = "2012-11-13 14:00:00:000";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d H:m:s:S");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date theDate = sdf.parse(dateString);

Note that Date.parse() is deprecated (so I did not recommend it).

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • The documentation states that in order to parse the `Z` (uppercase) format then you must specify a RFC 822 time zone (+XXXX or -XXXX). As per same documentation, `UTC` should theoretically only be parseable with the `z` (lowercase) format. I wouldn't be surprised if in reality `parse` treated the two interchangeably, but just to confirm (not having access to Groovy myself): does your first example above work? – vladr Nov 15 '12 at 03:57
  • Yes. Both examples worked for me (before I posted them). That being said, I don't have access to Groovy either (I've never used it). That's why I say: "_Here are two ways to do it in Java_". To be honest I don't really know what the relationship is between Java and Groovy. Maybe this answer is irrelevant.... – jahroy Nov 15 '12 at 03:59
1

I used Calendar to avoid timezone conversion. Although I did not use new Date(), the result is the same.

String dateString = "2012-11-13 14:00:00:000";
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d H:m:s:S");
calendar.setTime(sdf.parse(dateString));
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = calendar.getTime();
exody
  • 81
  • 1
  • 5
-1

Date class parse(String str) is deprecated from JDK 1.1, try SimpleDateFormat class that supports TimeZone and Locale settings too.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Pankaj
  • 5,132
  • 3
  • 28
  • 37