Joda: parse string as DateTime with weird timezone format - java

Is there a valid joda DateTimeFormat for date strings like the following:
Mon, 23 Jul 2018 07:08:26 +0300 GMT
I have tried:
DateTimeFormatter FMT1 = DateTimeFormat.forPattern("E, d MMM yyyy HH:mm:ss Z");
DateTimeFormatter FMT2 = DateTimeFormat.forPattern("E, d MMM yyyy HH:mm:ss z");
DateTimeFormatter FMT3 = DateTimeFormat.forPattern("E, d MMM yyyy HH:mm:ss z Z");
but none of these work.
I had a look in here https://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html and I cannot figure out a way to parse that date without having to change the string itself first.
Is there a way?

I take it that +0300 is the true offset and GMT is just there to say that +0300 is relative to GMT. Joda-Time supports apostrophes for delimiting constant text that shouldn’t be interpreted:
DateTimeFormatter formatter = DateTimeFormat.forPattern("E, d MMM yyyy HH:mm:ss Z 'GMT'");
DateTime dt = DateTime.parse("Mon, 23 Jul 2018 07:08:26 +0300 GMT", formatter);
System.out.println(dt);
Output on my computer in Europe/Copenhagen time zone:
2018-07-23T06:08:26.000+02:00
As an aside the idea of using a lowercase and an uppercase z wasn’t too bad. It works if you put them in the opposite order:
DateTimeFormatter formatter = DateTimeFormat.forPattern("E, d MMM yyyy HH:mm:ss Z z");
2018-07-23T04:08:26.000Z
I prefer the apostrophes, though. It’s hard for a reader to know what to expect from Z z.

Related

Java parse date from string with GMT string

I have the following date string Tue Feb 04 2020 16:11:25 GMT+0200 (IST) where I'm trying to convert into date time using the following code:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d yyyy HH:mm:ss O (zzz)", Locale.ENGLISH);
LocalDate dateTime = LocalDate.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);
And I got the following exception:
Text Tue Feb 04 2020 16:11:25 GMT+0200 (IST) could not be parsed at index 28
I look at the following SO question Java string to date conversion and I see that
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
So why I got the exception?
The following pattern works.
"E MMM d u H:m:s 'GMT'Z (z)"
You can replace Z with x or X for the same result.
You can spell it out, if you want, but it is not necessary.
"EEE MMM dd uuuu HH:mm:ss 'GMT'ZZZ (zzz)"
You should parse that input to an OffsetDateTime, since the input string includes a Date, a Time, and an Offset.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM d u H:m:s 'GMT'Z (z)", Locale.ENGLISH);
OffsetDateTime dateTime = OffsetDateTime.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);
System.out.println(dateTime);
Output
2020-02-04T16:11:25+02:00
One of the reasons is that you are trying to parse a datetime String (date, time, zone and offset) to an object (LocalDate) that only stores year, month and day, nothing more.
Use a suitable class, say ZonedDateTime and adjust the parsing pattern a little:
you can't use the localized offset O because in a DateTimeFormatter it doesn't support the formatting your String has, which is GMT+0200 and the formatter supports GMT+8; GMT+08:00; UTC-08:00; only (mind the colon). Use a combination of an escaped GMT plus a regular offset symbol x
you have a single d but a representation of days that will always have two digits, so you need to use dd
you have to escape the brackets the zone abbreviation is enclosed in and I think a single z is sufficient for such an abbreviation
Considering all these aspects, you could parse the String as follows:
public static void main(String[] args) {
String parsePattern = "EEE MMM dd yyyy HH:mm:ss 'GMT'x '('z')'";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(parsePattern,
Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);
System.out.println(zdt);
}
which then outputs (using the default formatter for ZonedDateTime)
2020-02-04T16:11:25+02:00[Asia/Jerusalem]
The problem here is the GMT+0200 if you use: GMT+02 it works.
But as already mentioned in the comments it is a little confusing that you use a variable called dateTime on something of the type LocalDate.
So your result will be only the date 2020-02-04 because LocalDate can only save this kind of data.
2 things - ZonedDateTime & missing ':' for 0
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss O (zzz)");
ZonedDateTime dateTime = ZonedDateTime.parse("Tue Feb 04 2020 16:11:25 GMT+02:00 (IST)", formatter);
This should work
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM d yyyy HH:mm:ss 'GMT'Z (z)", Locale.ENGLISH);
LocalDate dateTime = LocalDate.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);
Do not use a fixed text for the timezone:
Do not use a fixed text (e.g. 'GMT') for the timezone as mentioned in the existing answers because that approach may fail for other locales.
The recommended solution:
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "Tue Feb 04 2020 16:11:25 GMT+0200 (IST)";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("E MMM d u H:m:s VVZ (z)", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(strDateTime, dtf);
System.out.println(zdt);
}
}
Output:
2020-02-04T14:11:25Z[Atlantic/Reykjavik]
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
you wrote one d and you passed 04 you should write dd instead of d like the following
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd yyyy HH:mm:ss O (zzz)", Locale.ENGLISH);
LocalDate dateTime = LocalDate.parse("Tue Feb 04 2020 16:11:25 GMT+0200 (IST)", formatter);

How can I get the current date and time in below format , date = "Mon, 13 Jul 2020 14:08:30 GMT"

need help in java code to get current date and time in below format :
newdate = "Mon, 13 Jul 2020 14:08:30 GMT"
After this I need to replace current date with earlier one:
vJsonfile1.replace(earlierdate," "+ newdate);
You can use ZonedDateTime and the RFC_1123 format to get the output you need:
DateTimeFormatter dtf = DateTimeFormatter.RFC_1123_DATE_TIME;
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("GMT"));
System.out.println(dtf.format(zdt));
Wed, 15 Jul 2020 19:07:37 GMT
Note the 1123_DATE_TIME format doesnt play nice with North American Time zones so itll work as long as its GMT or European time zones otherwise below will suffice too:
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z");
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(dtf.format(zdt));
Wed, 15 Jul 2020 14:13:22 CDT
Which will output the current time with the time zone its in.
Here is an example that formats your datetime and then changes the date portion of it:
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println(zonedDateTime.format(DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z")));
zonedDateTime = ZonedDateTime.of(LocalDate.of(2020, 12, 15), zonedDateTime.toLocalTime(), zonedDateTime.getZone());
System.out.println(zonedDateTime.format(DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z")));
This code does exactly what you wanted it to do.
Output:
Mi, 15 Jul 2020 08:55:21 MESZ
Code:
SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z");
Date currentDate = new Date();
System.out.println(formatter.format(currentDate));

ParseException when parse date via SimpleDateFormat

I'm trying to parse date from string to Date
My string date is: Fri Apr 30 01:20:29 +0700 2010
My code is:
SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = format.parse(input);
But i'm getting an Unparsable date exception.
What's wrong?
Check your format, you only have one E instead of three :
EEE MMM dd HH:mm:ss Z yyyy
EDIT : also check your JVM's language locale or specify one for your call.
As writen in comments, following code works :
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);

Error parsing date string

I need to parse this string as a date:
Mon Jun 10 00:00:00 CEST 2013
Here is what I do:
SimpleDateFormat sdf = new SimpleDateFormat("ccc MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(dateString);
But I get a ParseException:
Unparseable date: "Wed Oct 02 00:00:00 CEST 2013" (at offset 0)
Any help please?
As others have said, you need EEE instead of ccc - but you should also specify a locale, so that it doesn't try to parse the month and day names (and other things) using your system default locale:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy",
Locale.US);
Your format is wrong. You need to use EEE instead of ccc, where E means Day name in week.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Have a look at the docs regarding all the valid patterns available for SimpleDateFormat.
Replace ccc with EEE in the pattern to specify the day of the week:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Example: https://gist.github.com/kmb385/8781482
Update the format as below :
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
It is a Locale problem. That's because dates are represented differently between Locales, so the JVM fires an exception if the Date is not in the correct format. You can solve it by setting a custom Locale:
String str = "Mon Jun 10 00:00:00 EST 2013";
Locale.setDefault(Locale.US);
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(str);
System.out.println(date);
IDEone examples do work because the default locale is Locale.US
java.time
The accepted answer uses SimpleDateFormat which was the correct thing to do in Feb 2014. In Mar 2014, the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern Date-Time API. Since then, it is highly recommended to stop using the legacy date-time API.
Note: Never use date-time formatting/parsing API without a Locale.
Solution using the modern date-time API:
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
class Main {
public static void main(String[] args) {
String stdDateTime = "Mon Jun 10 00:00:00 CEST 2013";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu", Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(stdDateTime, parser);
System.out.println(zdt);
}
}
Output:
2013-06-10T00:00+02:00[Europe/Paris]
If for any reason, you need an instance of java.util.Date, you can get it as follow:
Date date = Date.from(zdt.toInstant());
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.

Joda-Time DateTimeFormatter: get the correct output

I need to convert a Joda-Time DateTime into a String, in the following format:
Sat, 1 Jan 2011 00:00:00
I'm using this code:
DateTimeFormatter f = DateTimeFormat.forPattern("E, d MMM yyyy HH:mm:ss");
f.withLocale(Locale.US);
System.out.println(DateTime.now().toString(f));
My output is however:
wo, 20 jun. 2012 00:03:31
So the problems are:
The output is in Dutch instead of English ('wo' for 'wed')
The month has a period
How can I do this correctly?
DateTimeFormatter.withLocale returns a new DateTimeFormatter. So you need to do this:
f = f.withLocale(Locale.US);
Alternatively (equivalently):
DateTimeFormatter f = DateTimeFormat.forPattern("E, d MMM yyyy HH:mm:ss").withLocale(Locale.US);

Categories

Resources