Java: ZonedDateTime - parse timestring without timezone - java

I have a datetime-string WITHOUT a specified timezone.
But I want to parse it with ZonedDateTime to give it a timezone-meaning in the act of parsing.
This code is working but uses LocalDateTime for parsing - and then convert it to ZonedDateTime with giving it a timezone-meaning.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern ("yyyyMMddHHmm");
String tmstr = "201810110907";
LocalDateTime tmp = LocalDateTime.parse (tnstr,dtf);
ZonedDateTime mytime = ZonedDateTime.of (tmp, ZoneId.of ("UTC"));
Is there a way I can parse it directly with ZonedDateTime?
I have tried this, but it was not working.
mytime = mytime.withZoneSameInstant(ZoneId.of("UTC")).parse(str,dtf);

You may specify a default time zone on the formatter:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmm")
.withZone(ZoneId.of("UTC"));
String tmstr = "201810110907";
ZonedDateTime mytime = ZonedDateTime.parse(tmstr, dtf);
System.out.println(mytime);
Output:
2018-10-11T09:07Z[UTC]
Bonus tip: Rather than ZoneId.of("UTC") it’s usually nicer to use ZoneOffset.UTC. If you accept the output being printed as 2018-10-11T09:07Z instead (Z meaning UTC).

Related

Parse a String to LocaDateTime in Java

Consider a String "2022-03-23 21:06:29.4933333 +00:00".
How do I parse the above DateTimeOffset String to LocalDateTime in Java?
I tried with the following DateTimeFormatter but the format seems to be incorrect:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss\[.nnnnnnn\] \[+|-\]hh:mm\]");
LocalDateTime dateTime = LocalDateTime.parse(timestamp, formatter)
First, start by having the JavDocs for DateTimeFormatter at hand, this is going to really help determine which specifiers you need
The first thing to do is parse the text into a ZonedDateTime, LocalDateTime won't parse a input value with a time zone (AFAIK), you "might" be able to force it, but what's the point?
String text = "2022-03-23 21:06:29.4933333 +00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSSSSS z");
ZonedDateTime zdt = ZonedDateTime.parse(text, formatter);
System.out.println(zdt);
This prints...
2022-03-23T21:06:29.493333300Z
Now you could use ZonedDateTime#toLocalDateTime, but this won't take into account the current time zone of the user/computer.
If you need to convert the ZonedDateTime to LocalDateTime, it's best to do so in away which will translate the time (and date if required) to best represent the time within the current time zone (okay, I was confused typing it)
For example, converting the input value into my current time zone (+11 hours) would look like this...
ZoneId currentZone = ZoneId.systemDefault();
ZonedDateTime currentZDT = zdt.withZoneSameInstant(currentZone);
System.out.println(currentZDT);
LocalDateTime ldt = currentZDT.toLocalDateTime();
System.out.println(ldt);
which will print...
2022-03-24T08:06:29.493333300+11:00[Australia/Melbourne]
2022-03-24T08:06:29.493333300
This means that at 9:06pm on the 23rd March in Grinch (GMT), it was 8:06am on the 24th March where I live.
Now you can use different ZoneIds to convert to a TimeZone which is not the current computers TimeZone, but I'll leave that up to you to experiment with (for example, I used Convert ZonedDateTime to LocalDateTime at time zone to base my example on)
You need create custom DateTimeFormatter:
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
public class Main {
public static void main(String args[]){
String dateString = "2022-03-23 21:06:29.4933333 +00:00";
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(java.time.format.DateTimeFormatter.ISO_LOCAL_TIME)
.appendLiteral(' ')
.appendOffsetId()
.toFormatter();
//In case of OffSet matter, retaining the instant
LocalDateTime localDateTimeSavePointOfTime = OffsetDateTime.parse(dateString, formatter).withOffsetSameInstant(OffsetDateTime.now().getOffset()).toLocalDateTime();
//In case OffSet does not matter we can skip it
LocalDateTime localDateTimeSkipOffSet = LocalDateTime.parse(dateString, formatter);
}
}

Convert YYYY-MM-DDThh:mm:ssTZD format date string to local time

In my spring boot application I have to convert ISO 8601 datetime to localdatetime without using JODA. Currently what I am doing is
String receivedDateTime = "2019-11-13T00:11:08+05:00";
ZonedDateTime zonedDateTime = ZonedDateTime.parse(receivedDateTime);
DateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = new Date();
try {
date = utcFormat.parse(zonedDateTime.toString());
} catch (ParseException e) {
e.printStackTrace();
}
When I am using receivedDateTime with +00:00 like "2019-11-13T00:11:08+00:00" then it does not give any parsing error but not converting either. When I use +01:00 at the end then it also gives the parsing error.
UPDATE: 1
As per #Deadpool answer, I am using it like
String receivedDateTime = "2019-11-13T00:11:08+05:00";
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
.optionalStart().appendOffset("+HHMM", "0000").optionalEnd()
.toFormatter();
OffsetDateTime dt = OffsetDateTime.parse(receivedDateTime, formatter);
LocalDateTime ldt = dt.toLocalDateTime();
System.out.println(ldt);
and the the value of ldt it print is 2019-11-13T00:11:08.
UPDATE 2:
I tried using C# the same example and it gives me this date time {2019-11-12 11:11:08 AM}, which looks correct as the input time GMT +5 Hours and local time is EST America. So, when it converted it then it went back to 12th of Nov. Here is the code
var timeString = "2019-11-13T00:11:08+05:00";
DateTime d2 = DateTime.Parse(timeString, null, System.Globalization.DateTimeStyles.RoundtripKind);
Console.WriteLine("Hello World!" + d2);
UPDATE 3: So it boils down to following solution input String "2019-11-13T06:01:41+00:00" and output is local date "2019-11-13T00:01:41" Where system defauld ZoneId is "America/Chicago" which is -06:00 GMT
private LocalDateTime convertUtcStringToLocalDateTime(String UtcDateTime) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
.optionalStart().appendOffset("+HHMM", "0000").optionalEnd()
.toFormatter();
OffsetDateTime dateTime = OffsetDateTime.parse(UtcDateTime, formatter);
return dateTime.atZoneSameInstant(ZoneId.of(ZoneId.systemDefault().getId())).toLocalDateTime();
}
Using java.time alone this is simpler than you seem to think:
String receivedDateTime = "2019-11-13T00:11:08+05:00";
OffsetDateTime parsedDateTime = OffsetDateTime.parse(receivedDateTime);
ZonedDateTime dateTimeInMyTimeZone
= parsedDateTime.atZoneSameInstant(ZoneId.systemDefault());
System.out.println(dateTimeInMyTimeZone);
When I ran this in America/Toronto time zone, the output was:
2019-11-12T14:11:08-05:00[America/Toronto]
Since your string contains an offset, +05:00, and no time zone, like Asia/Karachi, use an OffsetDateTime for parsing it. Then convert to your local time zone using the atZoneSameInstant method. Even though you asked for your local time, don’t be fooled into using LocalDateTime. That class represent a date and time without any time zone, which is not what you need (and seldom needed at all).
Fortunately it’s easy to avoid the old classes SimpleDateFormat, DateFormat, TimeZone and Date. They were always poorly designed, the first two in particular are notoriously troublesome. They are all long outdated now. Instead get all the functionality we dream of from java.time, the modern Java date and time API.
What happened in your code?
Don’t use 'Z' in a format pattern string (and I repeat, don’t use SimpleDateFormat).
No matter if you use ZonedDateTime or OffsetDateTime, when you use toString with offset zero (as parsed from +00:00), the offset is printed as Z, which matches the 'Z' in your format pattern string, so your second parsing works. Only parsing once, converting back to string and parsing again is needlessly complicated. Worse when the original offset was +01:00 or +05:00. These are rendered the same again from toString, so don’t match 'Z', which caused your ParseException. Never use 'Z' in a format pattern string. Z denotes an offset of zero and needs to be parsed as an offset for you to get the correct result.
By using DateTimeFormatter you can customize the date format with different offset format by making them optional
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
.optionalStart().appendOffset("+HHMM", "0000").optionalEnd()
.toFormatter();
And the use the OffsetDateTime to parse string representing with offset
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
OffsetDateTime dateTime = OffsetDateTime.parse("2019-11-13T00:11:08+0000", formatter);
OffsetDateTime dateTime = OffsetDateTime.parse("2019-11-13T00:11:08+05:00", formatter);
If you want to convert it into local time zone time LocalDateTime then use atZoneWithSameInstant()
LocalDateTime local = dateTime.atZoneSameInstant(ZoneId.of("America/New_York")).toLocalDateTime()
Note : Don't use SimpleDateFormat and util.Date which are legacy old framework

DateTimeFormatter throws DateTimeParseException for date string

The following code is throwing a DateTimeParseException:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
ZonedDateTime dt = ZonedDateTime.parse(
"2019-01-01",
formatter.withZone(ZoneId.of("UTC"))
)
It also throws an exception with
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
ZonedDateTime dt = ZonedDateTime.parse(
"2019-01-01",
formatter)
)
As does
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
ZonedDateTime dt = ZonedDateTime.parse(
"2019-01-01",
formatter)
)
The SimpleDateFormat parser works just fine however -- I'm debating using it instead even though it's not thread safe and (I believe?) scheduled to be deprecated.
Obviously I'd prefer to use the java.time API, but I can't get this thing to work even after following the documented examples online. What do I do?
A ZonedDateTime must contain a date and a time; your input, 2019-01-01, contains only a date.
For that reason, you should use LocalDate in conjunction with LocalDate#atTime (to get a LocalDateTime object) and LocalDateTime#atZone (with ZoneOffset.UTC to get a ZonedDateTime).
var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
var zonedDateTime = LocalDate.parse("2019-01-01", formatter)
.atTime(1, 2, 3) // (hours, minutes, seconds)
.atZone(ZoneOffset.UTC);
The value of zonedDateTime is:
2019-01-01T01:02:03Z
ZoneDateTime or LocalDateTime always expect pair values of date and time. If you do not have time value, but have to build ZoneDateTime or LocalDateTime instance then you can choose some default time value like '00:00' and proceed as follow:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDate.parse("2019-01-01", formatter), LocalTime.MIN, ZoneId.of("UTC"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime localDateTime = LocalDateTime.of(LocalDate.parse("2019-01-01", formatter), LocalTime.MIN);
NOTE: java.time.LocalTime has some useful time constants
MIN('00:00'), MAX('23:59:59.999999999'), MIDNIGHT('00:00'), NOON('12:00')

How to convert UTC DateTime to another Time Zone using Java 8 library?

final Timestamp rawDateTime = Timestamp.valueOf("2031-04-25 18:30:00");
final ZoneId zoneId = ZoneId.of("Asia/Calcutta");
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
Instant.ofEpochMilli(rawDateTime.getTime()), zoneId);
// here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]
final ZonedDateTime zonedDateTime1 =
ZonedDateTime.of(rawDateTime.toLocalDateTime(), zoneId);
// here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]
But I want to get the converted date time as 2031-04-26 00:00:00+5:30 as my timestamp value is in the UTC Timezone.
Please help.
First, you should not use Timestamp. You can use DateTimeFormatter to parse into a LocalDateTime.
You then zone that LocalDateTime to UTC before converting to the Calcutta zone with ZonedDateTime.withZoneSameInstant.
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter();
LocalDateTime localDateTime = LocalDateTime.parse("2031-04-25 18:30:00", formatter);
ZoneId calcuttaZone = ZoneId.of("Asia/Calcutta");
ZonedDateTime calcuttaZonedDateTime = localDateTime.atZone(ZoneOffset.UTC)
.withZoneSameInstant(calcuttaZone);
Using DateTimeFormatter to format ZonedDateTime:
final Timestamp rawDateTime = Timestamp.valueOf("2031-04-25 18:30:00");
final ZoneId zoneId = ZoneId.of("Asia/Calcutta");
final ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
Instant.ofEpochMilli(rawDateTime.getTime()), zoneId);
// here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[XXX]");
System.out.println(formatter.format(zonedDateTime));
final ZonedDateTime zonedDateTime1 =
ZonedDateTime.of(rawDateTime.toLocalDateTime(), zoneId);
// here we are getting output as 2031-04-25T18:30+05:30[Asia/Calcutta]
System.out.println(formatter.format(zonedDateTime1));
The output:
2031-04-25 23:00:00+05:30
2031-04-25 18:30:00+05:30
Edited: according to the comment from #Ole V.V. - The local date time has to be converted to the zonedatetime , before applying the format :
final Timestamp rawDateTime = Timestamp.valueOf("2031-04-25 18:30:00");
LocalDateTime ldt = rawDateTime.toLocalDateTime();
final ZoneId zoneId = ZoneId.of("Asia/Calcutta");
ZonedDateTime zdt = ldt.atZone(ZoneId.of("UTC"))
.withZoneSameInstant(zoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[XXX]");
System.out.println(formatter.format(zdt));
This will give the output:
2031-04-26 00:00:00+5:30
Instead of ZonedDateTime with named zones having (supra-)national standards like day-time-savings, use OffsetDateTime.
OffsetDateTime utc = OffsetDateTime.parse("2031-04-25T18:30:00Z");
OffsetDateTime asia = utc.withOffsetSameInstant(ZoneOffset.ofHoursMinutes(5, 30));
The default parsing is for the ISO format.
Z means zero, UTC, +0:00.
The resulting default formatting is 2031-04-26T00:00+05:30.
After comment of Ole V.V.
The above is especially error prone if summer time is involved, like in Central European Time with varying offsets +1:00 and +2:00.
Instant raw = Instant.parse("2031-04-25T18:30:00Z");
ZonedDateTime zoned = raw.atZone(ZoneId.of("Asia/Calcutta"));
OffsetDateTime offset = OffsetDateTime.from(zoned);

ZonedDateTime parse exception

I am trying to convert string to ZonedDateTime.
I have tried following:
SimpleDateFormat zonedDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
zonedDateTimeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
long timeMs = zonedDateTimeFormat.parse("2017-07-18T20:26:28.582+03:00[Asia/Istanbul]").getTime();
It gives java.text.ParseException: Unparseable date
How can I parse the following string into ZonedDateTime
2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
The java.time API has many inbuilt-formats that simplify parsing and formatting process. The String you are trying to parse is in the standard ISO_ZONED_DATE_TIME format. So, you could parse it easily in the following way and then get the milliseconds from the epoch:
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME ;
ZonedDateTime zdt = ZonedDateTime.parse(
"2017-07-18T20:26:28.582+03:00[Asia/Istanbul]",
formatter); // prints 2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
long timeInMs = zdt.toInstant().toEpochMilli();
ZonedDateTime.parse seems to be designed to handle the exact string you provided. There is no need to go through the old SimpleDateFormat
For ZonedDateTime we need to use ZonedDateTime.parse method with DateTimeFormatter. If I am not wrong you have an ISO date:
ZonedDateTime zonedDateTime = ZonedDateTime.parse(
"2017-07-18T20:26:28.582+03:00[Asia/Istanbul]",
DateTimeFormatter.ISO_DATE_TIME
);
System.out.println(zonedDateTime); //2017-07-18T20:26:28.582+03:00[Asia/Istanbul]
You can use either ISO_ZONED_DATE_TIME or ISO_DATE_TIME. Both are able to parse a date-time with offset and zone.

Categories

Resources