java.text.ParseException: Unparseable date exception - java

If I do toString on a Date object, I am getting the output as below
2016-04-13 22:00:01.0
I am doing the below to convert the object to Data again
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd:HH:mm:SS");
Date convertedDate = (Date) formatter.parse(timestamp.toString());
But getting the ParseException.
I am trying to get the output as Date object as below
2016-04-13 22:00:01

This test should cover your example:
First from comment:
SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
Date convertedDate = formatter.parse("Thu Apr 14 15:24:14 CEST 2016")
System.out.print(convertedDate);
Second from question:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date convertedDate = formatter.parse("2016-04-13 22:00:01.0");
System.out.print(convertedDate);
It all depends on input String that you are receiving.
You should not use Date::toString method, use formatter, as you willl have no problem with zones and formats. It's better to have full flow control over your data.
Now if you want to convert it to a " 2016-04-13 22:00:01" format, simply use:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatedDate = formatter.format(convertedDate);

Parse from String with a certain date-format to a Date object:
String dateString = "2016-04-13 22:00:01.0";
SimpleDateFormat formatterFullMilliseconds = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" );
Date dateFromDateString = formatterFullMilliseconds.parse( dateString );
System.out.println( dateFromDateString ); // Output: Wed Apr 13 22:00:01 CEST 2016
Parse a java.util.Date object to the string representation you want:
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
System.out.println( formatter.format( dateFromDateString ) ); // 2016-04-13 22:00:01
System.out.println( formatter.format( new Date() ) ); // 2016-04-14 15:29:04

yyyy-MM-dd:HH:mm:SS
should be
yyyy-MM-dd HH:mm:ss.S

This is exactly what you need to do, to escape java.text.ParseException: Unparseable date exception
Date date = new Date();
SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = sp.parse(sp.format(date));
System.out.println(dt.toString());

Related

Parse yyyy-MM-DD String to Date yyyy-MM-DD in Android?

String startDateStr = "2017-02-03"
DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
2017-02-03 date is parsed to Tue Jan 03 00:00:00 GMT+05:45 2017
Did I
miss something?
Update
I needed a string to be converted to a date object
while maintaining the same format.
The reason for this is I want to make use of public boolean after(Date when) method
This will work ^_^
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
DateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
String startDateStr ="2017-02-03";
Date date = null;
try {
date = inputFormat.parse(startDateStr);
String startDateStrNewFormat = outputFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
Little explanation of your output :
D is Day in year (1-365)
d is day in month (1-31)
Check the document
Use SimpleDateFormat type for fomatter. You are creating DateFormat object but using SimpleDateFormat.
String startDateStr = "2017-02-03"
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
Date date = (Date)formatter.parse(startDateStr);
Yes you missed something. You used DD instead of dd in your yyyy-MM-DD format string. Here is how you do it:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(new Date());

Parse String Date time in java

I have a date time (which is a string) in the following format: 2/19/2015 5:25:35 p.m, and I wanted to turn it in the following Date Format: Thu Feb 19 5:25:35 p.m. CET 2015 I tried the following code:
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
Date date = format.parse (sDatePrecedenteExecution)
but I got the following error:
java.text.ParseException: unparseable Date: "2/19/2015 5:30:29 p.m."
Has java.text.DateFormat.parse (DateFormat.java:337)
You are currently using the "output" format to read your incoming date string (2/19/2015 5:25:35 p.m), which is why you see the error.
You need to specify a second format for parsing your incoming date string, and use that format to parse instead. It should look like this:
SimpleDateFormat inFormat = new SimpleDateFormat ("dd/MM/yyyy HH:mm:ss")
Date date = inFormat.parse(sDatePrecedenteExecution)
Note that you also have a bug in your output format - m means minutes, and you want MMM, which is months. Have a look at the docs.
Your SimpleDateFormat doesn't match the format which you are entering. They should reflect the same.
Try this code
String parseDate = ""19/02/2015 17:30:29";
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date parsedDate = dateFormat.parse(parseDate);
You need to change your code something like...
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
SimpleDateFormat format = new SimpleDateFormat ("dd/mm/yyyy HH:mm:ss");
try {
Date date = format.parse (sDatePrecedenteExecution);
System.out.println(date);
format = new SimpleDateFormat ("ddd d mmm yyyy HH: mm: ss");
String str = format.format(date);
System.out.println(str);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Try this pattern:
SimpleDateFormat format = new SimpleDateFormat ("dd mm yyyy HH:mm:ss");
You went wrong when you made: "ddd d mmm yyyy HH: mm: ss"
Use this pattern "dd/M/yyyy HH:mm:ss" instead & read the documentation
SimpleDateFormat format = new SimpleDateFormat ("dd/M/yyyy HH:mm:ss");
String sDatePrecedenteExecution = "19/02/2015 17:30:29";
try{date =format.parse (sDatePrecedenteExecution);
}catch(Exception ex){//deal with it here}
System.out.println(date.toString()); //Thu Feb 19 17:30:29 UTC 2015

Convert Current Timestamp to DD MMM YYYY format

I am trying to convert current timestamp to DD MMM YYYY format.
but i don't know why it is giving me an error unparsable.
code:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
System.out.println(format.format(fechaNueva));
working but I want to do
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
gives me error of unparsable.
java.text.ParseException: Unparseable date: "2014-10-07 15:38:29.876"
Following worked for me
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaNueva = format.parse(ts.toString());
format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(fechaNueva));
Output:
07 Oct 2014, 15:46
You were trying to parse date in a format you wanted it to be formatted instead of format in which it is already present.
Simply u can do this
Timestamp timestamp = new Timestamp(new Date().getTime());
System.out.println(timestamp);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println("Formatted "+dateFormat.format(timestamp));
hope this helps.
OutPut:-
Formatted 07 Oct 2014, 15:59
You can't use ts.toString(), you always need to use the same formatter.
Use just:
Timestamp ts = new Timestamp(new Date().getTime());
SimpleDateFormat format = new SimpleDateFormat("dd MMM YYY, HH:mm");
System.out.println(format.format(ts));

Getting the month and day from a TimeStamp

I have a String timeStamp in this format "Wed, 29 Jan 2014 20:14:15 GMT". I want to be able to compare this date with another date that is in this format 01/25/1999. I have tried simpledateformatter but with no success.
String a = connection.getHeaderField("Last-Modified"); //Returns Wed, 29 Jan 2014 20:14:15 GMT
Date lastModDate = new Date(file.lastModified()); //returns 01/25/1999
This is the simpleDateFormatter I tried implementing
SimpleDateFormat formatter;
Date dateIn = null;
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
try{
dateIn = (Date)formatter.parse(a);
}catch(ParseException e){
e.printStackTrace();
}
Log.d(TAG, "The server date is formated to : " + dateIn);
The dateIn is always null.
I want the to be able to do something like this
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date strDate = sdf.parse(valid_until);
if (new Date().after(strDate)) {
}
Use the following code...you will get the problem right.
Calendar calender = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String timeStamp = formatter.format(calender.getTime());
Date strDate = formatter.parse(timeStamp);
String currentTimeStamp = formatter.format(new Date());
Date currentTime = formatter.parse(currentTimeStamp);
if (currentTime.after(strDate)) {
}
Don't know what you tried but this should work:
String a = connection.getHeaderField("Last-Modified");
Date date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(a);
This can help http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
The reason is that you are using the wrong date format for your formatter. If the date you receive looks like
"Wed, 29 Jan 2014 20:14:15 GMT"
Then you should use the following format
formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
You should use the Calendar class and its subclass GregorianCalendar. For exampe, to get the month of your date:
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.get(Calendar.MONTH);

How to convert date and time stored in string to 24 format time in android?

I have my date and time stored in string i.e my string contains str ="18/01/2013 5:00:00 pm". How can I convert it to 24 format time in android?
You can use two SimpleDateFormat instances: one to parse the input as a date and a second to format the date as a string with the desired format.
For example, formattedDate in the code below will be 18/01/2013 17:00:00:
String str = "18/01/2013 5:00:00 pm";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
Date dt = input.parse(str);
SimpleDateFormat output = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String formattedDate = output.format(dt); //contains 18/01/2013 17:00:00
Notes:
hh is for Hour in am/pm (1-12) whereas HH is for Hour in day (0-23).
for more formatting options, check the javadoc
try
String str ="18/01/2013 5:00:00 pm";
Date date = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a").parse(str);
str = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(date);
System.out.println(str);
output
18/01/2013 17:00:00
To get AM PM and 12 hour date format use hh:mm:ss a as string formatter WHERE hh is for 12 hour format and a is for AM PM format.
Note: HH is for 24 hour and hh is for 12 hour date format
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
Example
String date = "18/01/2013 5:00:00 pm";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
You can use SimpleDateFormat for that:
SimpleDateFormat dateFormat = new SimpleDateFormat(dd/mm/yyyy HH:mm:ss");
Refer to this which was opposite to your requirement. Just posted the link so you might get the idea of difference that HH and hh makes.
Try using the java SimpleDateFormat class.
Example:
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
df.parse(date);
The upper case HH use a 24h format

Categories