Convert String to ZonedDateTime and change TimeZone - java

I have this string "Tue Apr 09 2019 12:59:51 GMT+0300"
I want to convert to ZonedDateTime.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss OOOO");
ZonedDateTime zdt = ZonedDateTime.parse(a, dtf);
After convert to ZonedDateTime, I want to change the timezone from GMT+0300 to other timezone.
My first problem is at parse. I get:
DateTimeParseException: Text 'Tue Apr 09 2019 12:59:51 GMT+0300' could not be parsed at index 25 (at GMT+0300, I think OOOO it's not right, but I don't know what else it is)
After that I don't know how to change the timezone.

OOOO expects the a colon before minute field, as the doc says:
Four letters outputs the full form, which is localized offset text,
such as 'GMT, with 2-digit hour and minute field, optional second
field if non-zero, and colon, for example 'GMT+08:00'.
You can insert a : before the last 00 programmatically, then parse it.

Since your string contains an offset and no time zone, what do you want a ZonedDateTime for? OffsetDateTime is more appropriate.
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
"EEE MMM dd yyyy HH:mm:ss 'GMT'xx", Locale.ROOT);
String a = "Tue Apr 09 2019 12:59:51 GMT+0300";
System.out.println(OffsetDateTime.parse(a, dtf));
2019-04-09T12:59:51+03:00
A time zone is a place on earth and encompasses historic and known future changes in UTC offset in that place. A time zone is conventionally given in the region/city format, for example Asia/Rangoon.
Edit
I use ZonedDateTime because I use time zone in my app.
I’m unsure exactly what you mean. Maybe you have decided in advance which time zone you are using? For example:
ZoneId zone = ZoneId.of("Europe/Zaporozhye");
OffsetDateTime odt = OffsetDateTime.parse(a, dtf);
ZonedDateTime zdt = odt.atZoneSameInstant(zone);
System.out.println(zdt);
2019-04-09T12:59:51+03:00[Europe/Zaporozhye]
If for some reason you want to regard GMT+0300 as a time zone even though it isn’t, the parsing I showed first works with ZonedDateTime too:
System.out.println(ZonedDateTime.parse(a, dtf));
2019-04-09T12:59:51+03:00

Related

Java Converting Retrieved TimeStamp to Instant Gives Wrong Day

I've made a simple method which is used to convert a timestamp retrieved from a database into a LocalDate. However for some reason I keep getting the wrong day in the conversion. I've shared the code below.
private LocalDate getLocalDateFromTimeStamp(Row row, String key){
return LocalDate.parse(row.getTimestamp(key).toInstant().atZone(ZoneOffset.UTC).toLocalDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
}
So the date I'm expecting is 2022-12-21 but what I get is 2022-12-22.
When I debug and check what
row.getTimestamp(key)
gets me a Date object of Wed Dec 21 20:47:46 CST 2022 which is what I expect.
When I check what
row.getTimestamp(key).toInstant()
does, I get "2022-12-22T02:47:46.299Z". And I think this is where the problem is popping up and I'm not sure why it's happening. The LocalDate that's returned by the method is "2022-12-22".
If anyone could shine a light on this I'd really appreciate it as I'm lost as to why this is happening.
Try it like this. Check out DateTimeFormatter for details on the following arguments.
String date = "Wed Dec 21 20:47:46 CST 2022";
EEE three letter day of week
MMM three letter month
dd integer day
HH:mm:ss time using 24 hour clock
z time zone name (CST)
yyyy year
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDate dt = LocalDateTime.parse(date, dtf).toLocalDate();
System.out.println(dt);
prints
2022-12-21
Updated
If you actually have an instance of Date you might try the following:
LocalDate ldt = LocalDate.ofInstant(date.toInstant(),
ZoneId.systemDefault());
tl;dr
Avoid unnecessary string manipulation. Use date-time types for date-time values.
myResultSet
.getObject( … , OffsetDateTime.class ) // For any database column of a data type akin to the SQL standard type of `TIMESTAMP WITH TIME ZONE`.
.toLocalDate() // Extract the date portion from the returned `OffsetDateTime` object.
.toString() // Generate text in standard ISO 8601 format.
Details
The Timestamp class is part of the terrible date-time classes that are now legacy. Use only their replacements, the modern java.time classes defined in JSR 310.
Instead of Timestamp, use OffsetDateTime with JDBC 4.2 and later. Do this for any database column of a data type akin to the SQL standard type of TIMESTAMP WITH TIME ZONE.
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
Your Question is not clear, but you seem to want the date portion of that moment as seen with an offset from UTC of zero hours-minutes-seconds.
The retrieved OffsetDateTime is likely already in UTC. But let’s be sure:
OffsetDateTime odtUtc = odt.withOffsetSameInstant( ZoneOffset.UTC ) ;
Extract the date portion.
LocalDate localDate = odtUtc.toLocalDate() ;
To generate text in standard ISO 8601 format, call toString.
String output = localDate.toString() ;

What is wrong with this java date formatter?

I'm trying to format the date you see as a String in the below code, to be able to have it as a LocalDateTime object, but I'm getting an exception, I'm following this guide https://mkyong.com/java8/java-8-how-to-convert-string-to-localdate/, but unfortunately it doesn't have and example like the date I have below, can someone please give me a hand here? I would really appreciate :)
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE MMM d hh:mm:ss zzz yyyy", Locale.US);
String date = "Wed Nov 18 00:00:00 COT 2020";
LocalDateTime localDateTime = LocalDateTime.parse(date, formatter);
System.out.println(localDateTime);
System.out.println(formatter.format(localDateTime));
I am getting:
java.time.format.DateTimeParseException: Text 'Wed Nov 18 00:00:00 COT
2020' could not be parsed at index 0
If you want round trip parsing/formatting with Zones try
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
String date = "Wed Nov 18 00:00:00 COT 2020";
ZonedDateTime localDateTime = ZonedDateTime.parse(date, formatter);
System.out.println(localDateTime);
System.out.println(formatter.format(localDateTime));
To answer the question as asked in the title, What is wrong with this Java date formatter?
EEEE is for full name of the day of the week like Monday. For the abbreviation like Mon you need either E, EE or EEE.
As others have said, lower case hh is for clock hour within AM or PM from 01 through 12, so cannot parse your hour of 00. And even if it could, it would not provide enough information for the time of day. For hour of day from 00 though 23 you need upper case HH.
There’s a more basic problem that Basil Bourque already mentioned in a comment: The result of parsing a time zone abbreviation like COT is generally undefined. While COT may have only one definition, I don’t know, most of the most common abbreviations are ambiguous, and you don’t know what you get from parsing them.
As has also been mentioned your formatter cannot be used for formatting a LocalDateTime. A LocalDateTime hasn’t got any time zone. The formatter requires a time zone for the abbreviated time zone name, zzz. You may either format a ZonedDateTime, or you may modify the formatter to have an override zone using its withZone method.
A tip: When you don’t know why parsing fails, try formatting the expected value with the same formatter and compare the result to the string you are trying to parse. Most often the difference will lead you on the right track. Like this:
ZonedDateTime val = ZonedDateTime.of(
2020, 11, 18, 0, 0, 0, 0, ZoneId.of("America/Bogota"));
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("EEEE MMM d hh:mm:ss zzz yyyy", Locale.US);
String toBeParsed = "Wed Nov 18 00:00:00 COT 2020";
String formatted = val.format(formatter);
System.out.println("String to be parsed: " + toBeParsed);
System.out.println("Formatted string: " + formatted);
Output:
String to be parsed: Wed Nov 18 00:00:00 COT 2020
Formatted string: Wednesday Nov 18 12:00:00 COT 2020
The difference are Wednesday and 12, so it seems the bugs in the pattern are at EEEE and hh.
Your string seems to come out of the toString method of the outdated java.util.Date class. To parse the string from there see one of the answers linked to at the bottom. Or still better, get hold of the Date object, convert it to a modern Instant using its toInstant method and perform any further conversions from there. Or yet still better, stop using Date completely.
Links
How to parse the result from Date.toString():
Answer by Arvind Kumar Avinash
Answer by Arvind Kumar Avihash
Answer by Arvind Kumar Avinash
My answer
Answer by Basil Bourque
And of course the answer by Scary Wombat to this question

How to convert Date String to joda DateTime?

I have a date string "Wed Nov 20 00:00:00 IST 2019". How do I convert it to joda DateTime with the pattern "yyyyMMdd".
dateObject.setStartDate(new DateTime().plusDays(1).toString("yyyyMMdd"));
The problem with your String pattern is, that JodaTime does not recognize the 'IST' timezone. (See http://joda-time.sourceforge.net/timezones.html for a list of supported time zones.)
If you always want to parse the date in the same time zone, you could use:
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss 'IST' yyyy").withZone(DateTimeZone.forID("Indian/Mahe"));
DateTime parsed = DateTime.parse("Wed Nov 20 00:00:00 IST 2019", dateTimeFormatter);
Note that I have used IST as a string literal in the pattern format, i.e., this will only work if your date strings always includes the "IST" string.
To add fixed time zone information to your parsed date use withZone on the formatter. I picked a random Indian timezone known to JodaTime, "Indian/Mahe" in this case. Look up the one that matches your time zone in the list of supported time zones.

Convert Date String Mon Mar 30 13:51:35 UTC 2015 to Date Object

I am trying to convert a string such as
String dateString = "Mon Mar 30 13:51:35 UTC 2015";
in a Date Object.
I tried this:
DateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println("Date Object:"+sdf.parse(dateString));
But the output of the date object is
Mon Mar 30 15:51:35 CEST 2015
as you can see:
1) it forwards the string's time ahead to two hours
2) it changes UTC --> CEST
I tried many solutions, but nothing worked. What is the correct way to do this?
EDIT: my objective here is to have a Date object from that original String. That Date Object should have the same parameters as the date string. In this case, the original hours of day (13) is turned to 15, but the desired is for it to stay at 13. I need this because in my program I will need to compare two different date objects.
EDIT: JAVA 8 SOLUTION
Searching the more recent Java 8, I found a better and more elegant solution. Here is the code
String pattern = "EEE MMM dd HH:mm:ss SSS zzz yyyy";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, Locale.UK).withZone(ZoneId.of("UTC"));
final ZonedDateTime parsed = ZonedDateTime.parse(dateString, formatter);
Furthermore, to compare it with, for example, the current time:
ZonedDateTime now = ZonedDateTime.now();
int compared = parsed.compareTo(now);
System.out.println("NOW:"+now.toLocalDateTime()+" PARSED:"+parsed.toLocalDateTime()+" COMPARED:"+compared);
You are doing it correctly. The date is being parsed correctly. You are just printing the date into your local computer timezone. When you do toString() to a date, prints the date in your local machine timezone.
Mon Mar 30 15:51:35 CEST 2015 == Mon Mar 30 13:51:35 UTC 2015
CEST is UTC +2
A java.util.Date does not have a time zone, practically speaking. There is a time zone inside but it cannot be set nor gotten. One of many poor design decisions made in these old date-time classes.
The Date::toString method applies your JVM’s current default time zone when generating the output string. Done with good intentions, but not helpful as it creates the illusion your Date object is in that zone when in fact it is not.
java.time
You are using a troublesome old legacy class, now supplanted by the java.time framework built into Java 8 and later.
Convert from a Date to an Instant, a moment on the timeline in UTC with a resolution of nanoseconds.
Instant instant = myJavaUtilDate.toInstant();
Call toString. The java.time classes use standard ISO 8601 formats when parsing/generating strings.
String output = instant.toString();
To create strings, convert from Instant to OffsetDateTime using the constant ZoneOffset.UTC. Then work with the java.time.format classes to generate the string.
OffsetDateTime odt = OffsetDateTime.ofInstant( instant , ZoneOffset.UTC );
Search Stack Overflow for more info and examples. These issues have addressed hundreds of times already.
Instead of UTC, use GMT when getting the timezone.
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Source
You already have the Date object. If you want to print it in format you want, you can use DateFormat to format the Date object as well:
Date date = sdf.parse(dateString);
System.out.println("Date Object:"+sdf.format(date));
// Use the date object ...

How can I use Java's SimpleDateFormat to parse a timezone given as "GMT+0100 (BST)"?

I have a date that's in the form of:
Wed Aug 17 2011 09:57:09 GMT+0100 (BST)
and have a filter that takes a time in a certain format. The problem seems to be the time zone on the end, none of the format strings I'm putting in the filter seem to work for this type of date format.
For example,
Wed Aug 17 2011 09:57:09 GMT+0100 (BST)
EEE MMM dd yyyy HH:mm:ss zZ?
The time zone part of this, keeps throwing an error.
Can anyone tell me what the correct format to parse the time zones on these dates is?
"z" needs a colon between hours and minutes. "Z" is only +/-HHMM (i.e. no "GMT" prefix).
One way to parse it is: EEE MMM dd yyyy HH:mm:ss 'GMT'Z. The "BST" bit is ignored, and it's based on assumption that there's always "GMT" before offset.
I would parse out and interpret the time zone information separately, then use that to construct the Date/Calendar object in the proper time zone.
The following code seems to work well enough with your example:
String source = "Wed Aug 17 2011 09:57:09 GMT+0100 (BST)";
String tzid = "GMT" + source.substring(28, 31)
+ ":" + source.substring(31, 33);
TimeZone tz = TimeZone.getTimeZone(tzid);
// if (tz == null) ?
SimpleDateFormat f = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss");
f.setTimeZone(tz);
Date date = f.parse(source);
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println(date);
Prints "Wed Aug 17 08:57:09 UTC 2011".
A more sophisticated approach would be to use regex to extract individual parts ("+/-", "hh" and "mm") of the time zone offset.
Alternatively, you can attempt to discern the 3-letter time zone id (the string in between ( and )), and use the corresponding Java TimeZone if it exists.
In your particular example, though, "BST" resolves to Bangladesh Time which is GMT+0600 so you're better off with the numeric offset. "BST" here should probably be taken as British Summer Time (GMT+0100). This can be important because numeric offsets do not indicate the use of daylight savings time, which can be in effect depending on the date.
A more heuristic routine could take this into account and attempt to resolve the name first, but verify that the GMT offsets match, and fallback on the simple "GMT+hh:mm" timezones otherwise.
If you can not find a pattern matching your use case, try:
try{
new Date("Wed Aug 17 2011 09:57:09 GMT+0100 (BST)")
}catch(Exception e)
{
// Parse exception
}

Categories

Resources