I have the String 11/08/2013 08:48:10
and i use SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
and when im parsing it throws an exception : unparseable date
what's wrong with it ?
String result = han.ExecuteUrl("http://"+han.IP+":8015/api/Values/GetLastChange");
Log.d("Dal","result date time "+result); #result is 11/08/2013 08:48:10
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date convertedDate = new Date();
try
{
convertedDate = dateFormat.parse(result);
}
catch (ParseException e)
{
e.printStackTrace();
}
Its working try parse your date like this..
String dtStart = "11/08/2013 08:48:10";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
date = format.parse(dtStart);
System.out.println("Date ->" + date);
} catch (ParseException e) {
e.printStackTrace();
}
Working code is here.
You can use below code to convert from String to Date
String myStrDate = "11/08/2013 08:48:10";
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date date = format.parse(myStrDate);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
For reverse, it means, to convert from Date to String
SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
try {
Date date = new Date();
String datetime = myFormat.format(date);
System.out.println("Current Date Time in give format: " + datetime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
For more reference, regarding Date and Time formatting. Visit developer site
It’s not the answer that you asked for, but it should be the answer that other readers want in 2020 and onward.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/uuuu HH:mm:ss");
String result = "11/08/2013 08:48:10";
LocalDateTime dateTime = LocalDateTime.parse(result, formatter);
System.out.println("Parsed date and time: " + dateTime);
Output from this snippet is:
Parsed date and time: 2013-11-08T08:48:10
The Date
class that you used is poorly designed and long outdated, so don’t use that anymore. Instead I am using java.time, the modern Java date and time API. If you need a Date
for a legacy API that you cannot afford to upgrade just now, the conversion is:
Instant i = dateTime.atZone(ZoneId.systemDefault()).toInstant();
Date oldfashionedDate = DateTimeUtils.toDate(i);
System.out.println("Converted to old-fashioned Date: " + oldfashionedDate);
Converted to old-fashioned Date: Fri Nov 08 08:48:10 CET 2013
what's wrong with it ?
The only thing wrong with your code is you’re using the notoriously troublesome SimpleDateFOrmat
and the poorly designed Date
class. Which wasn’t wrong when you asked the question in 2013. java.time didn’t come out until 4 months later.
Your code is running fine. One may speculate that a leading space or the like in your string has prevented parsing. If this was the problem, try parsing result.trim()
rather than just result
since trim()
returns a string with the leading and trailing whitespace removed.
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
Date
is a little simpler: Date.from(i)
.org.threeten.bp
with subpackages.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With