java.text.ParseException: Unparseable date
Your pattern does not correspond to the input string at all... It is not surprising that it does not work. This would probably work better:
SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
Then to print with your required format you need a second SimpleDateFormat:
Date parsedDate = sdf.parse(date);
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss");
System.out.println(print.format(parsedDate));
Notes:
- you should include the locale as if your locale is not English, the day name might not be recognised
- IST is ambiguous and can lead to problems so you should use the proper time zone name if possible in your input.
How to solve java.text.ParseException: Unparseable date?
you are parsing a string that is not a correct representation of that pattern, you are missing the TimeZone... something like: -0600
example:
Date date1 = df.parse("2016-11-18T11:13:43.838-0600Z");
here is the doc for more info....
your code should look like:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
Date date1 = df.parse("2016-11-18T11:13:43.838-0600Z");
DateFormat outputFormatter1 = new SimpleDateFormat("dd-MMM-yyyy");
String output1 = outputFormatter1.format(date1); //
} catch (ParseException e) {
e.printStackTrace();
}
java.text.ParseException: Unparseable date: November 17, 2022 13:33
The date format should be like following
MMMM d,yyyy h:mm
not
MMMM d,yyyy,h:mm
java.text.ParseException: Unparseable date: 2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)
You have used m
for the month which is wrong. You have to use M
for the month and m
for the minute.
I also recommend you use DateTimeFormatter instead of using the outdated SimpleDateFormat
. Check this to learn more about the modern date/time API.
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("d/M/yyyy hh:mm:ss a (zzz)")
.toFormatter(Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse("2/9/2016 10:30:00 AM (GMT-05:00)", formatter);
System.out.println(zdt);
}
}
Output:
2016-09-02T10:30-05:00[GMT-05:00]
Also,
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("d/M/yyyy hh:mm:ss a (zzz)")
.appendLiteral(" Eastern Time (US & Canada)")
.toFormatter(Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse("2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)",
formatter);
System.out.println(zdt);
System.out.println(formatter.format(zdt));
}
}
Output:
2016-09-02T10:30-05:00[GMT-05:00]
2/9/2016 10:30:00 AM (GMT-05:00) Eastern Time (US & Canada)
java.text.ParseException: Unparseable date. dd MMM yyyy HH:mm:ss format
Try with:
String time = "24 Apr 2021 11:56:44";
Date timeOnLine = new SimpleDateFormat("dd MMM yyyy HH:mm:ss", Locale.US).parse(time);
How to parse date correctly java.text.ParseException: Unparseable date: 2021-09-02T13:16:00.0000000Z?
Parse the quotes too; use java.time.Instant
Like many others I recommend that you use java.time, the modern Java date and time API, for your date and time work.
Your string contains double quotes first and last. You can deal with them in two ways:
- If there’s a way that you can get your string from Bing search without the quotes, do that. Then
Instant.parse()
will parse your string, and you’re done. - Otherwise java.time can parse the quotes too.
For parsing the quotes use the following formatter:
private static final DateTimeFormatter BING_INSTANT_PARSER
= new DateTimeFormatterBuilder().appendLiteral('"')
.append(DateTimeFormatter.ISO_INSTANT)
.appendLiteral('"')
.toFormatter();
Then parse like this:
String stringFromBing = "\"2021-09-02T13:16:00.0000000Z\"";
Instant instant = BING_INSTANT_PARSER.parse(stringFromBing, Instant::from);
System.out.println("String to parse: " + stringFromBing);
System.out.println("Result: " + instant);
Output:
String to parse: "2021-09-02T13:16:00.0000000Z"
Result: 2021-09-02T13:16:00Z
Which java.time class to use?
Assuming that your string always comes with the Z
at the end, denoting UTC, Instant
is the correct class to use. OffsetDateTime
and ZonedDateTime
will work too, but I consider them overkill. You don’t want to use LocalDateTime
since you would then throw away the essential information that the string is in UTC.
Link
Oracle tutorial: Date Time explaining how to use java.time.
Related Topics
Why Do We Usually Use || Over |? What Is the Difference
Conditionally Ignoring Tests in Junit 4
What Is the "Continue" Keyword and How Does It Work in Java
Java 8: Difference Between Two Localdatetime in Multiple Units
Using Regular Expressions to Extract a Value in Java
How to Set the Default Locale in the Jvm
Cut Out Image in Shape of Text
Scale the Imageicon Automatically to Label Size
Update Jlabel Every X Seconds from Arraylist<List> - Java
Differences Between Oracle Jdk and Openjdk
Resource Leak: 'In' Is Never Closed
How to Instantiate a Class by Name in Java
Is There a Concurrent List in Java's Jdk