ParseException when parsing a date of yyyy-MM-dd format - java

private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
Date date = dateFormatMonth.parse(strtdate[0]);
strdate[0] contains "2018-06-11"
I'm getting a unparsable exception on this line:
java.text.ParseException: Unparseable date: "2018-06-11"

You are getting the error because you are using the wrong pattern to parse the date. Use this instead:
private SimpleDateFormat dateFormatMonth = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
Date date = dateFormatMonth.parse(strtdate[0]);
Assuming "06" is the month and "11" is day.

Related

Java - Unparseable date exception

I have a date object which has date in the format: 2015-09-21 10:42:48:000
I want it to be displayed on the UI in this format.21-Sep-2015 10:42:48
The code I am using is not working and throws this:
Unparseable date exception: Unparseable date: "2015-09-21 10:42:48"
Here is the actual code:
String createdOn=f.getCreatedOn().toString();//f.getCreatedOn() returns a date object
SimpleDateFormat format=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
Date date=format.parse(createdOn.substring(0,createdOn.length()-3));
log.debug(">>>>>>date now is: "+date);
model.addAttribute("date", date);
model.addAttribute("info", messages);
SimpleDateFormat format1=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
format1.format(date);
log.debug(">>>>>>date now is again: "+date);
Unparseable date exception: Unparseable date: "2015-09-21 10:42:48"
Since your input date format is yyyy-MM-dd HH:mm:ss. But you are trying parsing with dd-MMM-yyyy HH:mm:ss format.
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//change input date format here
Date date=format.parse("2015-09-21 10:42:48:000");
//Date date=format.parse(createdOn);//Here no need of subtracting 000 from your date
SimpleDateFormat format1=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
System.out.println(format1.format(date));
SimpleDateFormat doc
Your input date format is different.
String createdOn="2015-09-21 10:42:48:000";
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=format.parse(createdOn.substring(0,createdOn.length()-4));
System.out.println(">>>>>>date now is: "+date);
SimpleDateFormat format1=new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
format1.format(date);
System.out.println(">>>>>>date now is again: "+date);
You are using wrong format to display and parse.
// We will use this for parsing a string that represents a date with the format declared below. If you try to parse a date string with a different format, you will get an exception like you did
SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// This is for the way we want it to be displayed
SimpleDateFormat displayFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
// Parse the date string
Date date = parseFormat.parse("2015-09-21 10:42:48:000");
// Format the date with the display format
String displayDate = displayFormat.format(date);
System.out.println(">>>>>>date now is: " + displayDate);

UnParseable Date Exception :"2015-02-06T16:05:20"

I have a Date Variable "StartTime", in which i need to store this input String "2015-02-06T16:05:20"
I tried like below, but it gives Unparsable Date Exception. What i am doing wrong?
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String inputTime = "2015-02-06T16:05:20";
setStartTime(dateFormat.parse(inputTime));
You can change you date format to "yyyy-MM-dd'T'HH:mm:ss"
Read more Java SimpleDateFormat

Convert String "04-OCT-14 03.47.24.342000000 PM" to java.util.Date

String dateFromDb = "04-OCT-14 03.47.24.342000000 PM";
SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss",Locale.ENGLISH);
Date Date1 = datetimeFormatter1.parse(dateFromDb);
Timestamp fromTS1 = new Timestamp(Date1.getTime());
While converting this exception is thrown:
java.text.ParseException: Unparseable date: "04-OCT-14 03.47.24.342000000 PM" at java.text.DateFormat.parse(DateFormat.java:337)
Can anyone help?
Your dateformater configuration is wrong.
Use this pattern:
SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat("dd-MMM-yyyy HH.mm.ss.SSSSS aa",Locale.ENGLISH);
For more information see the javadoc of SimpleDateFormat

convert string to Mysql time stamp in servlet java

hi i am passing my date time string using get method in apache servlet and it goes like this
http://localhost:8084/example/Time_ser?date=15/03/2013%2004:14:30%20PM
and i am using
String time=request.getParameter("date");
to get the date value.....
and my java code to convert the string to timestamp is given below
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss aa");
java.util.Date date = (java.util.Date)formatter.parse(time);
Timestamp timets = new Timestamp(date.getTime());
but it shows error like this
java.text.ParseException: Unparseable date: "15/03/2013 04:14:30 PM"
am i doing anything wrong please help me..........
use / not - because you date is formatted as 15/03/2013 04:14:30 PM.
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss aa");

Java parse exception

I created a hibernate program and when i tried to format the date i am getting the below mentioned error:
java.text.ParseException: Unparseable date
SimpleDateFormat formatter=new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd");
Date date1=null;
Date date2=null;
Date startdate=(Date)formatter.parse(startDate);
Date enddate=(Date)formatter.parse(endDate);
How to resolve the parse exception?
Thanks in advance.
You are trying to parse startdate but it doesn't actually contain any date and hence the Exception.
You should try:
String strDate = "2012-10-20";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = formatter.parse(strDate);

Categories