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);
Related
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.
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
I have date string as "02-SEP-2012". I want to convert it to 8/02/2012 format.
String deliverydate="02-SEP-2012";
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
Date date=sdf.parse(deliveryDate);
This gives me
java.text.ParseException: Unparseable date: "01-Sep-2012"
How to parse this?
I want to insert this date in MS SQL.
2018/Java 10
String deliverydate="02-SEP-2012";
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.appendPattern("dd-MMM-yyyy")
.toFormatter(Locale.UK);
LocalDate ld = LocalDate.parse(deliverydate, formatter);
System.out.println(ld);
Original answer
Look at the two Strings, 02-SEP-2012 and MM/dd/yyyy - these are not the same format.
The date parser is expecting the String to be in the same format as the formatter
String deliverydate="02-SEP-2012";
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yyyy");
Date date=sdf.parse(deliveryDate);
sdf=new SimpleDateFormat("MM/dd/yyyy");
System.out.println(sdf.format(date));
You should use:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Date date = sdf.parse(deliveryDate);
then to re-format:
sdf.applyPattern("MM/dd/yyyy");
System.out.println(sdf.format(date));
in sql server:
You could use convert() function..
You can try
Insert into <table> (date_col,<othercolumns>)
select CONVERT(date,'02-SEP-2012'),<othercolumns>
Example:
declare #date varchar(20) ='02-SEP-2012'
select CONVERT(date,#date)
result:
2012-09-02
Change the SimpleDateFormat
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yyyy");
String deliverydate="02-SEP-2012";
SimpleDateFormat sdf=new SimpleDateFormat("MM-dd-yyyy");
Date date=sdf.parse(deliveryDate);
Did you try this "- " instead of "/".
Using the parse(String source) method can throw java.text.ParseException exception if the supplied date is not in a valid format.So you should use valid format.
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);
I have used this code to parse a String received from Json in Date Format.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date myDateConference = sdf.parse(jsonO.getString("myDateConference"));
}
catch(ParseException e){
e.printStackTrace();
}
The String of the date is something in this form 2012-08-02T00:00:00
But I get this exception
java.text.ParseException: Unparseable date: "2012-08-02T00:00:00"
What is wrong?
use
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
instead of
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
There is a T in your source String:
"2012-08-02<b>T</b>00:00:00"
Change the format to
yyyy-MM-dd'T'HH:mm:ss