ISO8601 to DateTime with time zone information preserved - java

Below is a deserialization of an ISO8601 date string that contains time zone information. Notice that the time zone information is lost:
scala> val date1 = new DateTime().withZone(DateTimeZone.forID("Europe/Berlin"))
date1: org.joda.time.DateTime = 2013-09-22T18:42:15.348+02:00
scala> date1.getZone()
res45: org.joda.time.DateTimeZone = Europe/Berlin
scala> val date2 = new DateTime(date1.toString())
date2: org.joda.time.DateTime = 2013-09-22T19:42:15.348+03:00
scala> date2.getZone()
res46: org.joda.time.DateTimeZone = Europe/Vilnius
scala> date1.getZone() == date2.getZone()
res47: Boolean = false
Time zone information (UTC offset) is serialized, as in +03:00 and +02:00 at the end of the ISO8601 strings, but it is lost after deserialization. As you can see the date2 DateTime object, which I expected to be a copy of date1 has the system's UTC offset instead of +02:00, which date1 had.
How do I deserialize an ISO8601 string as to preserve the UTC offset?

The constructor you are using, new DateTime(Object instant), (actually passed through to BaseDateTime) doesn't parse, instead it converts the given object (in your case, a String).
Long story short, it uses the default time zone:
The constructor considers the passed parameter an Instant and requests an InstantConverter from ConverterManager
The constructor calls getInstantMillis() on that StringConverter
That method actually does use a standard ISO 8601 DateTimeFormatter, however instead of parse it calls parseMillis().
parseMillis, as you can see from the javadocs, returns a date in the default time zone.
Use DateTime.parse instead:
DateTime date2 = DateTime.parse(date1.toString());
// 2013-09-22T19:21:48.461+02:00

Related

Convert the TimeStamp from an API to local TimeStamp

I need a help with the code.
My API has TimeStamp in UTC format and I need to convert it to my local TimeStamp i.e., CST.
For example:
MY API has TimeStamp value as : 2019-01-08T13:17:53.4225514 (which is in UTC).
I need the output to be like Jan 8, 2019 8:28:18.514 AM (Which is in CST my local Time )
How to convert it in local TimeStamp?
Timestamp createdOn = api.getCreatedOn(); (HERE I get the TimeStamp as Object from api)
Turns out a bit difficult to do right after all.
Here's how you parse a String timestamp in UTC to obtain a ZonedDateTime object of your preferred time zone:
// define formatter once to be re-used wherever needed
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd'T'HH:mm:ss") // all fields up seconds
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true) // handle variable-length fraction of seconds
.toFormatter();
String text = "2019-01-08T13:17:53.4225514";
LocalDateTime localTime = LocalDateTime.parse(text, formatter); // parse string as a zone-agnostic LocalDateTime object
ZonedDateTime utcTime = localTime.atZone(ZoneId.of("UTC")); // make it zoned as UTC zoned
ZonedDateTime cstTime = utcTime.withZoneSameInstant(ZoneId.of("America/Chicago")); // convert that date to the same time in CST
// print resulting objects
System.out.println(utcTime);
System.out.println(cstTime);

How to convert ISO 8601 formatted DateTime to another time zone in Java?

My current date:
Date utc: 2018-06-06T16:30:00Z (ISO 8601 in UTC)
OR
Date iso: 2018-06-06T11:30:00-05:00 (ISO 8601)
OR
Date epoch: 1528302600000 (Epoch/Unix Timestamp)
I wish to convert the above DateTime to some another time zone areas (like GMT+5:30). And I'm not sure which time format I'll receive from above three. So can I've a generic method which can convert above to some another time zone returning java.util.Date in Java 8?
I did Something like this, But it didn't worked out
public Date convertDateToLocalTZ(Date iso8601, ZoneId toZoneId) {
Date dateTime = null;
if (iso8601 != null && toZoneId != null) {
Instant instant = iso8601.toInstant();
LocalDateTime localDateTime = instant.atZone(toZoneId).toLocalDateTime();
dateTime = Date.from(localDateTime.atZone(toZoneId).toInstant());
return dateTime;
}
return dateTime;
}
Since question is tagged java-8 use java.time API.
UPDATE: For version 4 of question where 2018-06-06T11:30:00-05:00 was added.
To parse 1528302600000, you parse it into a long, then use Instant.ofEpochMilli().
To parse a format like 2018-06-06T11:30:00-05:00, you can using OffsetDateTime or ZonedDateTime. Both can also parse 2018-06-06T16:30:00Z.
To change the time zone specifically to a particular offset like GMT+5:30, use ZoneOffset, e.g. ZoneOffset.of("+05:30"), or ZoneId, e.g. ZoneId.of("GMT+05:30").
Note 1: GMT+5:30 is not valid.
Note 2: To change to the time zone of a region, honoring Daylight Savings Time, use e.g. ZoneId.of("Asia/Kolkata").
To parse all 3 input formats, and even support the extended format like 2018-06-06T11:30-05:00[America/Chicago], use ZonedDateTime, with special handling for the epoch number.
public static ZonedDateTime parseToZone(String text, ZoneId zone) {
if (text.indexOf('-') == -1)
return Instant.ofEpochMilli(Long.parseLong(text)).atZone(zone);
return ZonedDateTime.parse(text).withZoneSameInstant(zone);
}
The caller can then decide if only the offset, not the full time zone, should be used, by converting it to OffsetDateTime using toOffsetDateTime().
Test
ZoneId india = ZoneId.of("Asia/Kolkata");
System.out.println(parseToZone("2018-06-06T16:30:00Z", india));
System.out.println(parseToZone("2018-06-06T11:30:00-05:00", india));
System.out.println(parseToZone("1528302600000", india));
System.out.println(parseToZone("1528302600000", india).toOffsetDateTime());
Output
2018-06-06T22:00+05:30[Asia/Kolkata]
2018-06-06T22:00+05:30[Asia/Kolkata]
2018-06-06T22:00+05:30[Asia/Kolkata]
2018-06-06T22:00+05:30
Original Answer
Use the parse() method with 2018-06-06T16:30:00Z.
Use the ofEpochMilli() method with 1528302600000.
Then use atZone() to convert to your desired time zone.
Demo
Instant instant1 = Instant.parse("2018-06-06T16:30:00Z");
Instant instant2 = Instant.ofEpochMilli(1528302600000L);
ZoneId india = ZoneId.of("Asia/Kolkata");
ZonedDateTime date1 = instant1.atZone(india);
ZonedDateTime date2 = instant2.atZone(india);
System.out.println(instant1);
System.out.println(instant2);
System.out.println(date1);
System.out.println(date2);
Output
2018-06-06T16:30:00Z
2018-06-06T16:30:00Z
2018-06-06T22:00+05:30[Asia/Kolkata]
2018-06-06T22:00+05:30[Asia/Kolkata]
To print the result in human format, use a DateTimeFormatter.
DateTimeFormatter indiaFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
.withLocale(Locale.forLanguageTag("en-IN"));
DateTimeFormatter hindiFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)
.withLocale(Locale.forLanguageTag("hi-IN"));
System.out.println(date1.format(indiaFormatter));
System.out.println(date1.format(hindiFormatter));
Output
6 June 2018 at 10:00:00 PM IST
6 जून 2018 को 10:00:00 अपराह्न IST
In Java 8+, you should use the new java.time API.
Your initial UTC time must be modelized as an Instant. Use DateTimeFormatter to parse from a string like 2018-06-07T22:21:00Z if needed, or get the current Instant with Instant.now.
Then you can use Instant.atZone or Instant.withOffset to convert to a ZonedDateTime resp. OffsetDateTime with the desired time shift. ZonedDateTime helps you get the date/time at a given region/country, while OffsetDateTime makes a purely numerical time shift independent from location and daylight saving time.

Unable to convert UTC to IST, it is still returning me UTC only

I am trying to convert UTC Date to IST. But to my surprise, after converting everything, it is still returning me UTC only. How is it possible?
INPUT:
StartDateTimeUtc='2017-09-15T14:00:00',
EndDateTimeUtc='2017-09-15T15:00:00'
Code:
public static final String DATE_FORMATE_CURRENT = "yyyy-MM-dd'T'HH:mm:ss";
Date meetingStartDate = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(model.StartDateTimeUtc);
Date meetingEndDate = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(model.EndDateTimeUtc);
//Convert Date to String
DateFormat df = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT);
String meetinStartDateString = df.format(meetingStartDate);
String meetingEndDateString = df.format(meetingEndDate);
//Convert String Date to IST
SimpleDateFormat dftwo = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT);
dftwo.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
Date date = null;
Date datetwo = null;
try {
date = dftwo.parse(meetinStartDateString);
datetwo = dftwo.parse(meetingEndDateString);
} catch (ParseException e) {
e.printStackTrace();
}
dftwo.setTimeZone(TimeZone.getDefault());
String formattedStartDate = dftwo.format(date);
String formattedEndDate = dftwo.format(datetwo);
//Convert String Date back to Date format so that we can pass into Calendar code
Date meetingStartDateFinal = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(formattedStartDate);
Date meetingEndDateFinal = new SimpleDateFormat(Constants.DATE_FORMATE_CURRENT, Locale.ENGLISH).parse(formattedEndDate);
OUTPUT again in UTC:
Start Date : Fri Sep 15 14:00:00 GMT+05:30 2017
End Date : Fri Sep 15 15:00:00 GMT+05:30 2017
A java.util.Date doesn't have any timezone information. It just contains one value: the number of milliseconds since unix epoch (1970-01-01T00:00Z, or January 1st 1907, at midnight in UTC).
This number of milliseconds is the same, everywhere in the world. What's is different is the corresponding date and time in each timezone. Example: right now, this millis value is 1505481835424, which corresponds, in UTC, to 2017-09-15T13:23:55.424Z. This same value corresponds to 10:23 AM in São Paulo, 18:53 in Kolkata, 14:23 in London and so on. The local date/time is different in each timezone, but the millis value is the same for everyone.
That's why you don't convert a Date itself: the millis value is the same, and there's no need to change it. What you can change is the representation of this date in different timezones.
SimpleDateFormat, by default, uses the JVM default timezone to parse dates. But if you know that the inputs are in a specific zone, you must set in the formatter. So, to parse your inputs, you must do:
String startDateTimeUtc = "2017-09-15T14:00:00";
String endDateTimeUtc = "2017-09-15T15:00:00";
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss");
// input is in UTC
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
// parse dates
Date meetingStartDate = sdf.parse(startDateTimeUtc);
Date meetingEndDate = sdf.parse(endDateTimeUtc);
The 2 Date objects above will correspond to 14:00 and 15:00 UTC (which is the same as 19:30 and 20:30 in Kolkata timezone).
But if you just print the Date objects directly (using System.out.println, logging, or even checking their values in a debugger), it'll implicity call the toString() method, which uses the JVM default timezone behind the scenes, resulting in the output you're seeing (Fri Sep 15 14:00:00 GMT+05:30 2017).
If you want to print in a specific format, and in a specific timezone, you'll need another formatter:
// another formatter for output
SimpleDateFormat outputFormat= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
// output will be in Asia/Kolkata timezone
outputFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
System.out.println(outputFormat.format(meetingStartDate));
System.out.println(outputFormat.format(meetingEndDate));
The output will be:
2017-09-15T19:30:00
2017-09-15T20:30:00
Which corresponds to the same UTC dates in Kolkata timezone.
Just remember: you don't convert the Date's between timezones (because their millis values are "absolute" - they are the same for everyone in the world). You just change the String representation of those dates (the corresponding date/time in a specific timezone).
Java new Date/Time API
The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.
In Android you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. To make it work, you'll also need the ThreeTenABP (more on how to use it here).
This new API has lots of different date/time types for each situation. In this case, the inputs have date and time, but no timezone information, so first I parse them to a org.threeten.bp.LocalDateTime, using a org.threeten.bp.format.DateTimeFormatter:
// parse the inputs
DateTimeFormatter fmt = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
LocalDateTime startDt = LocalDateTime.parse(startDateTimeUtc, fmt);
LocalDateTime endDt = LocalDateTime.parse(endDateTimeUtc, fmt);
Then I use a org.threeten.bp.ZoneOffset to convert them to UTC, and later a org.threeten.bp.ZoneId to convert this to another timezone. The result will be a org.threeten.bp.ZonedDateTime:
// input is in UTC
ZoneOffset utc = ZoneOffset.UTC;
// convert to Asia/Kolkata
ZoneId zone = ZoneId.of("Asia/Kolkata");
ZonedDateTime start = startDt.atOffset(utc).atZoneSameInstant(zone);
ZonedDateTime end = endDt.atOffset(utc).atZoneSameInstant(zone);
Then I use the same DateTimeFormatter to format the output:
System.out.println(fmt.format(start));
System.out.println(fmt.format(end));
The output is:
2017-09-15T19:30:00
2017-09-15T20:30:00
Note that I don't need to set the timezone in the formatter, because the timezone information is in the objects (they are responsible to do the conversion).

Creating a datetime object in a particular timezone in JodaTime

I have a date, timezone and time as Strings, and I want to construct a JodaTime object with that date and time for that timezone. The code I have written is
String dateString = "2016-06-02";
String time = "01:00:00";
String timezone = "Australia/Brisbane";
DateTime dateInTimezone = DateTime.parse(dateString+" "+time,DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"))
.withZone(DateTimeZone.forID(timezone));
I would expect dateInTimezone to be created with value 2016-06-02T01:00:00.000+10:00 but it gets created with value 2016-06-02T18:00:00.000+10:00. My systems timezone is in America\Los_Angeles. From what I understand it takes the time I pass as a parameter in my systems timezone and converts that to the timezone I specify. Probably thats why 01:00:00 got internally converted to 18:00:00.
How do I create a joda time object with specified time and specified timezone, without any conversions?
Add the timezone to your formatter before parsing:
String dateString = "2016-06-02";
String time = "01:00:00";
String timezone = "Australia/Brisbane";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
.withZone(DateTimeZone.forID(timezone));
DateTime dateInTimezone = DateTime.parse(dateString+" "+time, formatter);
Another option is to do it your way, but call DateTime.withZoneRetainFields(), but then there's the risk of the parse failing if it falls out on an invalid date (e.g. between a DST gap) in the initial timezone.

How to convert Joda-Time DateTime to java.util.Date and vice versa?

Is it possible to do that? If yes, then how do I do the conversion from Joda-Time to Date and vice versa?
To convert Java Date to Joda DateTime:-
Date date = new Date();
DateTime dateTime = new DateTime(date);
And vice versa:-
Date dateNew = dateTime.toDate();
With TimeZone, if required:-
DateTime dateTimeNew = new DateTime(date.getTime(), timeZone);
Date dateTimeZone = dateTime.toDateTimeAtStartOfDay(timeZone).toDate();
You haven't specified which type within Joda Time you're interested in, but:
Instant instant = ...;
Date date = instant.toDate();
instant = new Instant(date);
// Or...
instant = new Instant(date.getTime());
Neither Date nor Instant are related to time zones, so there's no need to specify one here.
It doesn't make sense to convert from LocalDateTime / LocalDate / LocalTime to Date (or vice versa) as that would depend on the time zone being applied.
With DateTime you can convert to a Date without specifying the time zone, but to convert from Date to DateTime you should specify the time zone, or it will use the system default time zone. (If you really want that, I'd specify it explicitly to make it clear that it's a deliberate choice.)
For example:
DateTimeZone zone = DateTimeZone.forID("Europe/London");
Date date = ...;
DateTime dateTime = new DateTime(date.getTime(), zone);
To Convert from Java Date to Joda Time of Date:
To convert from Date to DateTime time zone needed to be specified.
To convert from java.util Date to Joda Time of Date you just need to pass the java.util Date and time zone to the constructor of Joda Time of Date.
java.util.Date date = new java.util.Date(System.currentTimeMillis());
DateTimeZone dtz = DateTimeZone.getDefault();// Gets the default time zone.
DateTime dateTime = new DateTime(date.getTime(), dtz);
To Convert from Joda Time of Date to Java Date:
For the reverse case Joda DateTime has a method toDate() which will return the java.util Date.
DateTime jodaDate = new DateTime();
java.util.Date date = jodaDate.toDate();
For More Details Visit Here

Categories