I receive a datetime string containing an ISO8601 datetime, like this "2001-07-04T12:08:56.235-07:00", then this string is parsed to a jodatime datetime object this way new DateTime("2001-07-04T12:08:56.235-07:00"), and after that it is converted to a string again using a variable formatter pattern passed by arguments, but when that happens, no timezone is used, so the system's default timezone is being used. What I want is to extract the timezone (or offset) from the first given date, and use it to print it accordingly. Is it possible?
Thanks beforehand!
Don't use new DateTime("..."). Use DateTime.parse("...").
See difference:
DateTime dateTime1 = new DateTime("2001-07-04T12:08:56.235-07:00");
System.out.println(dateTime1);
System.out.println(dateTime1.getZone());
DateTime dateTime2 = DateTime.parse("2001-07-04T12:08:56.235-07:00");
System.out.println(dateTime2);
System.out.println(dateTime2.getZone());
Output (I'm in eastern US)
2001-07-04T15:08:56.235-04:00
America/New_York
2001-07-04T12:08:56.235-07:00
-07:00
As you can see, using new converts to default time zone, while using parse retains the given time zone.
Related
I have this simple code:
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm yyyy-MM-dd");
DateTime dateTime = formatter.withZone(DateTimeZone.forID("America/New_York")).parseDateTime("08:30 2015-06-01");
DateTime dateTime2 = formatter.withZone(DateTimeZone.forID("America/New_York")).parseDateTime("08:30 2015-12-01");
these are leap times. when I hit toString method, I got something like this:
2015-06-01T08:30:00.000-04:00
2015-12-01T08:30:00.000-05:00
which is correct, we can see UTC time - offset. But when I call getHourOfDay, I got 8 and not 4/3 as expected. What am I doing wrong? Please, share some advices here.
Well, from the Javadoc for DateTimeFormatter#withZone():
Returns a new formatter that will use the specified zone in preference to the zone of the printed object, or default zone on a parse.
So, you told the formatter to use the specific timezone on parsing AND output, and the input you gave it did NOT contain a timezone, so this is the expected result. In essence you said:
Here's a date string without timezone, parse it assuming America/New_York
Convert the date back to String, in the timezone America/New_York
This is what it did.
I am reading strings from a file that look like "2015-06-06T01:51:49-06:00" into a Joda-Time DateTime object. But DateTime isn't behaving the way I want.
import org.joda.time.DateTime;
System.out.println("2015-06-06T01:51:49-06:00");
System.out.println(new DateTime("2015-06-06T01:51:49-06:00"));
The result is the following
2015-06-06T01:51:49-06:00
2015-06-06T00:51:49.000-07:00
Later on I need the hour and minutes. Here that would be 1:51. But DateTime is printing it out in a different timezone I'm guessing? How can I get DateTime to print out 2015-06-06T01:51:49.000-06:00
A DateTime stores a time zone, but the DateTime(Object instant) constructor first converts the String to an instant (millis), thereby losing the timezone information, so it applies the default time zone to that instant.
To retain time zone, use DateTime.parse(String str):
System.out.println("2015-06-06T01:51:49-06:00");
System.out.println(new DateTime("2015-06-06T01:51:49-06:00"));
System.out.println(DateTime.parse("2015-06-06T01:51:49-06:00"));
Output
2015-06-06T01:51:49-06:00
2015-06-06T03:51:49.000-04:00
2015-06-06T01:51:49.000-06:00
I receive a string date from another system and I know the locale of that date (also available from the other system). I want to convert this String into a Joda-Time DateTime object without explicitly specifying the target pattern.
So for example, I want to convert this String "09/29/2014" into a date object using the locale only and not by hard coding the date format to "mm/dd/yyyy". I cant hard code the format as this will vary depending on the local of the date I receive.
String localizedCalendarDate = DateTimeFormat.shortDate().print(new LocalDate(2014, 9, 29));
// uses in Germany: dd.MM.yyyy
// but uses in US: MM/dd/yyyy
LocalDate date =
DateTimeFormat.mediumDate().withLocale(Locale.US).parseLocalDate("09/29/2014");
DateTime dt = date.toDateTimeAtStartOfDay(DateTimeZone.forID("America/Los_Angeles"));
As you can see, you will also need to know the clock time (= start of day in example) and time zone (US-California in example) in order to convert a parsed date to a global timestamp like DateTime.
I need to parse a string into a Joda-Time DateTime (or java.util.Date.) This is an example of the string I'm getting:
eventDateStr = 2013-02-07T16:05:54-0800
The code I'm using:
DateTimeFormatter presentation = DateTimeFormat.forPattern("yyyy-MM-dd kk:mm:ssZ");
DateTime eveDate = presentation.parseDateTime(eventDateStr);
The above throws this exception:
Invalid format: "2013-02-07T16:05:54-0800" is malformed at "T04:03:20-0800"
So I'm parsing the 'T' out of there:
eventDateStr = eventDateStr.indexOf("T") > 0 ? eventDateStr.replace("T", " ") : eventDateStr;
and trying again. This time no exception but the time zone is off:
2013-02-08T02:05:54.000+02:00
Note the difference: in the original string the timezone is '-0800' and here it's '+02:00'. This in turn changes the entire date, which is now a day later.
What am I doing wrong?
Call the method withOffsetParsed on the DateTimeFormatter object to get a DateTimeFormatter that keeps the time zone parsed from the String, instead of offsetting it to the local time zone.
Regarding why T is shown when you print out the DateTime, Basil Bourque has a nice explanation in the comment below.
Regarding T, a DateTime is not a string nor does it contain a string. A DateTimeFormatter instance can generate a string representation of the date, time, and time zone information stored within a DateTime. When you invoke the toString method on a DateTime (either implicitly or explicitly), a built-in formatter based on ISO 8601 is used automatically. That formatter uses YYYY-MM-DDTHH:MM:SS.ssssss+00:00 format.
I am getting a date/time string from web in the format of "yyyy/mm/dd'T'HH:MM:SS'Z'" and it is in UTC.
Now I have to identify the current time zone of device and then convert this time to my local time..
How do I do it?
(FYI, Currently, UTC time is 10:25 AM, in India current time is 3:55 PM)
Try using TimeZone.getDefault() instead of TimeZone.getTimeZone("GMT")
From the docs:
... you get a TimeZone using
getDefault which creates a TimeZone
based on the time zone where the
program is running.
EDIT: You can parse date using SimpleDateFormat (there is also the documentation on the format string there). In your case, you want to do (untested):
// note that I modified the format string slightly
SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd'T'HH:mm:ss'Z'");
// set the timezone to the original date string's timezone
fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = fmt.parse("1998/12/21T13:29:31Z", new ParsePosition(0));
// then reset to the target date string's (local) timezone
fmt.setTimeZone(TimeZone.getDefault());
String localTime = fmt.format(date);
alternatively, use two separate instances of SimpleDateFormat, one for original and one for target time.