Java: unparseable date exception
What you're basically doing here is relying on Date#toString()
which already has a fixed pattern. To convert a Java Date
object into another human readable String pattern, you need SimpleDateFormat#format()
.
private String modifyDateLayout(String inputDate) throws ParseException{
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").parse(inputDate);
return new SimpleDateFormat("dd.MM.yyyy HH:mm:ss").format(date);
}
By the way, the "unparseable date" exception can here only be thrown by SimpleDateFormat#parse()
. This means that the inputDate
isn't in the expected pattern "yyyy-MM-dd HH:mm:ss z"
. You'll probably need to modify the pattern to match the inputDate
's actual pattern.
Update: Okay, I did a test:
public static void main(String[] args) throws Exception {
String inputDate = "2010-01-04 01:32:27 UTC";
String newDate = new Test().modifyDateLayout(inputDate);
System.out.println(newDate);
}
This correctly prints:
03.01.2010 21:32:27
(I'm on GMT-4)
Update 2: as per your edit, you really got a ParseException
on that. The most suspicious part would then be the timezone of UTC
. Is this actually known at your Java environment? What Java version and what OS version are you using? Check TimeZone.getAvailableIDs()
. There must be a UTC
in between.
Simpledateformat unparseable date
Use this date formatter method I have created
public static String dateFormater(String dateFromJSON, String expectedFormat, String oldFormat) {
SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
Date date = null;
String convertedDate = null;
try {
date = dateFormat.parse(dateFromJSON);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(expectedFormat);
convertedDate = simpleDateFormat.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return convertedDate;
}
and call this method like
dateFormater(" 01/04/2018" , "EE dd MMM yyyy" , "dd/MM/yyyy")
and you will get the desired output
Change date format Java getting Unparseable date Error
Change the format to
SimpleDateFormat dt = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date lastUpdatedDateFormatted = dt.parse(lastUpdatedDate);
System.out.println(lastUpdatedDateFormatted);
And it should be able to parse the date then.
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);
Unparseable date - in java
Your formatter is set up to handle the format "EEE, d MMM yyyy HH:mm:ss Z"
. "1604328483716"
isn't remotely in that format.
The value "1604328483716"
looks like the string version of a milliseconds-since-The-Epoch value. If so, convert it to a long
(Long.parseLong
) and use new Date(theLongValue)
, which will give you a Date
instance for Monday November 2nd 2020 14:48:03 GMT (or whatever that is in your local timezone).
You might also consider using the newer date/time API in the java.time
package, rather than java.util.Date
.
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)
Unparseable date Exception java date pattern
Use Locale.US
so that it considers the month names in US format
SimpleDateFormat("dd-MMM-yyyy",Locale.US)
See this code run live at IdeOne.com.
How to resolve Unparseable date Exception in Java 8 with Date format yyyy-MM-dd hh:mm a
java.time
I recommend that you use java.time, the modern Java date and time API, for your date and time work.
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu\u2013MM\u2013dd");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
String dateString = "2020–03–01";
String timeString = "03:15 PM";
LocalDate date = LocalDate.parse(dateString, dateFormatter);
LocalTime time = LocalTime.parse(timeString, timeFormatter);
LocalDateTime dateTime = date.atTime(time);
System.out.println(dateTime);
Output:
2020-03-01T15:15
With java.time it is straightforward to combine date and time after parsing, so I prefer to parse them individually.
What went wrong in your code?
Credits go to Ajeetkumar for noticing and reporting in comments: The hyphen in your date string is not a usual minus sign or hyphen with character value 2D hexadecimal (45 decimal), but a en dash with character value 2013 hexadecimal (8211 decimal). So when you specify a usual hyphen in your format pattern string, they don’t match, and parsing fails. Instead I am using a Unicode escape for putting the en dash into the format pattern string. Simply pasting it in there would have worked too (provided that you save your .java file with a character encoding that supports it), but I wanted to make the reader aware that something special was going on here, so I preferred the Unicode escape with \u
.
There is another problem with your code: You are not providing any locale for your formatter. So it uses the default locale of your JVM. As long as that locale expects PM
, parsing will work. If one day you change your locale setting or run your code on a computer or JVM with a different default locale, parsing will suddenly fail, at you may have a hard time figuring out why. I have specified English locale for parsing the time. Some would prefer doing it for the date too even though technically it isn’t necessary.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Unicode Character 'EN DASH' (U+2013) on FileFormat.info.
Related Topics
How to Parse Command Line Arguments in Java
What Do ^ and $ Mean in a Regular Expression
What Is the Concept of Erasure in Generics in Java
Draw a Line in a Jpanel With Button Click in Java
Difference Between Declaring Variables Before or in Loop
How to Sort by Two Fields in Java
Is It Safe to Use a Static Java.Sql.Connection Instance in a Multithreaded System
Normalization in Dom Parsing With Java - How Does It Work
How to Prevent Java.Lang.Numberformatexception: For Input String: "N/A"
Why Do This() and Super() Have to Be the First Statement in a Constructor
How Many Ways to Click on Webelement in Webdriver
How to Set Time Zone of a Java.Util.Date
Fastest Way to Determine If an Integer'S Square Root Is an Integer
Difference Between Javac and the Eclipse Compiler
Json Parsing Using Gson For Java