Cannot parse String into Date from Json, UnparsableDateException - java

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

Related

How to get proper written date using SimpleDateFormat in Java

I have this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date result = dateFormat.parse(this.getCreatedTime());
Basically I want to convert a string like "2016-09-27T09:19:57Z" into something like "September 27, 2016 at 9:19 AM".
If I use the code above I end up with a Date object, but all the methods are deprecated. So how do I achieve this?
You can use DateFormat again as #Thomas wrote:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date inputDate = inputFormat.parse(this.getCreatedTime());
DateFormat outputFormat = new SimpleDateFormat("outputFormat");
String output = outputFormat.format(inputDate);
You should do research before posting any question here.
Use this to get Date from your required pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
Date date = null;
try {
date = format.parse(unformattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Make new instance of your desired patter.
format = new SimpleDateFormat("MMMM dd, yyyy 'at' HH:mm a", Locale.getDefault());
String formattedDate = format.format(date);

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

Parse and format Date in Android

I'm having a problem in parsing a date. I'm new on android and try to search for the solution but it seems no luck. I'd already tried to follow this one http://developer.android.com/reference/java/text/SimpleDateFormat.html but still got an error.
please help me..
I try to parse this date 2014-03-18T02:07:35.742-0400 and try to format to this 03/18/2014 02:07
I got this error:
java.lang.IllegalArgumentException
at java.text.DateFormat.format(DateFormat.java:361)
at java.text.Format.format(Format.java:93)
Try something like this:
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2014-03-18T02:07:35.742-0400");
System.out.println(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(date));
For my time zone prints:
03/18/2014 10:07
private final DateFormat parsedFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.getDefault());
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
Date date = dateFormat.parse(dateFormat.format(your_date));
Date parsedDate = parsedFormat.parse(parsedFormat.format(date));
Reference
use following code
try{
String srcDate = new String("2014-03-18T02:07:35.742-0400");
SimpleDateFormat srcDf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
SimpleDateFormat destDf = new SimpleDateFormat("yyyy/MM/dd");
Date date = srcDf.parse(srcDate);
// format the date into another format
dateStr = destDf.format(date);
}catch (ParseException e) {
// TODO: handle exception
}

Formatting a date String

I have a string like this:
"2013-06-25 10:00:09"
I want to format it like this:
"25/06/2013 10:00".
I'm trying this:
SimpleDateFormat sdf = new SimpleDateFormat(""dd/MM/yyyy kk:mm"")
Date d;
try {
d = sdf.parse(data_publicao_db[totalCount_ + i]);
String s2 = (new SimpleDateFormat("yyyyMMdd")).format(d);
Log.d("Data test", "" + s2);
}
catch (ParseException e) { e.printStackTrace(); }
But it keeps giving a
ParseException: Unparseable date
Is there a better and right way to this?
sdf should be
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
the other SimpleDateFormat you need to reformat your date:
new SimpleDateFormat("dd/MM/yyyy HH:mm")
Let's go!
//your date String and SDF in your String format
String originalDateString = "2013-06-25 10:00:09";
SimpleDateFormat originalSdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//get the Date from the String
Date date = originalSdf.parse(originalDateString);
//Wanted output format
SimpleDateFormat wantedSdf= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//print it
System.out.println(wantedSdf.format(date));
Since data_publicao_db[totalCount_ + i] is looking like 2013-06-25 10:00:09, you have to parse it with a format of yyyy-MM-dd HH:mm:ss .
To convert the result to the format 25/06/2013 10:00, you have to format it with a SimpleDateFormat using the format dd/MM/yyyy HH:mm.

Categories