Parsing dates without timezone conversion
Asked Answered
B

3

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)?

Bias answered 15/11, 2012 at 2:18 Comment(5)
well, new Date().parse("yyyy-M-d H:m:s:S" + " Z", '2012-11-13 14:00:00:000' + ' 0000') comes to mind...Uniaxial
@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"Bias
@Bias @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.Amir
@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? :)Uniaxial
@Uniaxial - My apologies... I overlooked it (hard to read code in comments sometimes).Recessive
R
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).

Recessive answered 15/11, 2012 at 2:27 Comment(2)
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?Uniaxial
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....Recessive
A
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();
Adore answered 30/6, 2021 at 14:54 Comment(0)
V
-1

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

Vermeil answered 15/11, 2012 at 2:22 Comment(1)
You're thinking of the Java method. Groovy provides a replacement that lets you specify a format.Amir

© 2022 - 2024 — McMap. All rights reserved.