ParseException: Unparseable date in Java [duplicate] - java

This question already has answers here:
Java - Unparseable date
(3 answers)
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 3 years ago.
I need to convert String (in date format) to TimeStamp.
IN first I did like this.
private Timestamp dateConverter(String date) {
Timestamp timestamp = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm");
Date parsedDate = null;
try {
parsedDate = dateFormat.parse(date);
timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
Result was: ParseException: Unparseable date: "16 Jan 2020 11:02"
I tried by another way :
private Timestamp dateConverter(String date) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm");
LocalDateTime ldt = LocalDateTime.parse(date , f);
return Timestamp.valueOf(ldt);
}
At this time I have Internal Server Error with DateTimeParseException: Text '16 Jan 2020 11:02' could not be parsed at index 3

Your pattern can't know what is the language of your month, English, French, Arabic, Russian, Mandarin..., to solve this you have to help the formatter by using Locale like so :
DateTimeFormatter f = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm")
.withLocale(new Locale("us"));
LocalDateTime ldt = LocalDateTime.parse("16 Jan 2020 11:02" , f); // 2020-01-16T11:02

Related

SimpleDateFormat: Unparseable date when I try parse month: MMM dd, yyyy [duplicate]

This question already has answers here:
Cannot parse date string containing three letter month
(1 answer)
Java - Unparseable date
(3 answers)
Closed 1 year ago.
I try to parse string do the Date format, but get Unparserable exception:
public void roundTest() throws ParseException {
String pattern = "MMM dd, yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
String from = "Jul 01, 2021";
Date result = sdf.parse(from);
}
The same for java.time package:
public void roundTest() throws ParseException {
String from = "Jul 01, 2021";
String pattern = "MMM dd, yyyy";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
System.out.println(LocalDate.parse(from, formatter));
}
Error: "java.time.format.DateTimeParseException: Text 'Jul 01, 2021' could not be parsed at index 0"
Where the error?

Why I get a wrong result when parsing a date from string with SimpleDateFormat ? (Java) [duplicate]

This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
How does Java "week year" really work?
(2 answers)
Closed 2 years ago.
I'm trying to parse a date from string but i get a wrong date result and dont understand why :/
String dateStr = "September 6, 2013 - 10:48";
SimpleDateFormat parser = new SimpleDateFormat("MMMM dd, YYYY - HH:mm", Locale.US);
Date date = parser.parse(dateStr);
When I look date result (in debugger) i see : Sun Dec 30 10:48:00 CET 2012
Can someone tell me where I'm wrong please ?
String dateStr = "September 6, 2013 - 10:48";
SimpleDateFormat parser = new SimpleDateFormat(
"MMMM dd, yyyy - hh:mm", Locale.US);
Date date = parser.parse(dateStr);
System.out.println(date);
Use yyyy and not YYYY
Also don't use Date but use LocalDate and LocalDateTime.
Here is how you would do it using LocalDateTime
String dateStr = "September 6, 2013 - 10:48";
DateTimeFormatter format = DateTimeFormatter.ofPattern(
"MMMM d, y - HH:mm",Locale.US);
LocalDateTime date = LocalDateTime.parse(dateStr,format);
System.out.println(date.format(format));
Note the HH is for 24 hour time since you didn't include an AM or PM in your date string.

How to parse this date format "Wednesday, 12 June 2019 14:23:39" to this "2019-03-05T11:56:13Z" in Java? [duplicate]

This question already has answers here:
Java: How to convert string with month name to DateTime?
(1 answer)
How to get current moment in ISO 8601 format with date, hour, and minute?
(23 answers)
Closed 3 years ago.
How do I parse this date "Wednesday, 12 June 2019 14:23:39" which comes as a String to a date format like this "2019-03-05T11:56:13Z" in JAVA?
You should use the java.time API for this:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM yyyy HH:mm:ss", Locale.ENGLISH);
LocalDateTime localDateTime = LocalDateTime.parse("Wednesday, 12 June 2019 14:23:39", dateTimeFormatter);
String isoDateTime = localDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(isoDateTime);
which results in:
2019-06-12T14:23:39
attached example of how you can parsar your date
greetings
public static void main(String[] args) throws ParseException {
String date_s = "2011-01-18 00:00:00.0";
// *** note that it's "yyyy-MM-dd hh:mm:ss" not "yyyy-mm-dd hh:mm:ss"
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = dt.parse(date_s);
// *** same for the format String below
SimpleDateFormat dt1 = new SimpleDateFormat("E, dd MMM yyyy hh:mm:ss");
System.out.println(dt1.format(date));
}
enter image description here

convert string to date with locale [duplicate]

This question already has answers here:
Changing String date format
(4 answers)
Closed 4 years ago.
I'm trying to convert a string to date but every time i do it keeps throwing errors at me, Im not sure what i am missing
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
// m brings in all variables from a get/setter
date = format.parse(m.getEventTime());
} catch (ParseException e) {
e.printStackTrace();
Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(date.toString());
My variable m.getEventTime() is passing the following string 2018-04-28 14:00:00
I have tried passing the string as 2018-04-28T14:00:00Z to no avail.
No errors are coming from the stack trace from the try/catch block but the log is printing out D/Response.Error.Date: 2017-08-19 15:00:00
when i add e.toString() to the log it prints out D/Response.Error.Date: 2017-08-19 15:00:00 java.text.ParseException: Unparseable date: "2017-08-19 15:00:00"
On the actual application when run the time is shown as now Sat Apr 28 08:22:33 GMT+01:00 2018
Am i missing something?
You can try this,
Date date = new Date();
SimpleDateFormat oldformat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
try {
// m brings in all variables from a get/setter
date = oldformat.parse(m.getEventTime());
} catch (ParseException e) {
e.printStackTrace();
Log.d("Response.Error.Date", m.getEventTime());
}
eventTime.setText(format.format(date));
You can get time format like this:
String dateTime = null;
DateFormat df = new SimpleDateFormat("dd-MM-yyyy, hh:mm a");
//here you can use your required format
dateTime = df.format(new Date(stringTime));
You are converting date into English which is causing the exception:
Please Change:
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a", Locale.ENGLISH);
eventTime.setText(date.toString());
with
SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yy hh:mm a");
eventTime.setText(format.format(date));

Unable to Parse the Date, ParseException is thrown [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
Unable to parse the given date
String DT = "14 Jun 2016 09:54:02 GMT";
DateFormat simpleDateFormat = new SimpleDateFormat("dd MM yyyy HH:mm:ss z");
Date date = simpleDateFormat.parse(DT);
after this I want to convert to CST Time in this format 13-JUN-16 08.53.43
Exception StackTrace
java.text.ParseException: Unparseable date: "14-Jun-2016 09:54:02 GMT" at java.text.DateFormat.parse(Unknown Source) at package2.TimeZone.parseTime(TimeZone.java:16) at package2.TimeZone.main(TimeZone.java:10)
Date format should be like as below:
String DT = "14 Jun 2016 09:54:02 GMT";
DateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yy HH:mm:ss z");
Date date = (Date) simpleDateFormat.parse(DT);
After converting date format, it should pass like below:
String newstring = new SimpleDateFormat("dd-MMMM-yy HH:mm:ss").format(date).toString().toUpperCase();
System.out.println(newstring);
To parse:
You are using the wrong mask to parse it, it should be as follows(with MMM instead of MM):
DateFormat simpleDateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
Date date = simpleDateFormat.parse(DT);
To format it to the desired format:
DateFormat sdf= new SimpleDateFormat("dd-MMM-yy HH.mm.ss");
sdf.setTimeZone(TimeZone.getTimeZone("US/Central"));
String formattedDate = sdf.format(date).toUpperCase();
Without the uppercase it will show 13-jun-16 08.53.43 instead of 13-JUN-16 08.53.43

Categories

Resources