I am trying to parse "updated_time" and trying to convert it in Date() object. But I am getting following exception.
java.text.ParseException: Unparseable date: "2015-10-11T07:21:14+0000"
Here is my code.
private Date convertStringToDate(String createdAt) {
Date convertedDate = new Date();
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
convertedDate = formatter.parse(createdAt);
} catch (ParseException e) {
Log.e(TAG, "parse exception while converting string to date for facebook : "+e.toString());
}
return convertedDate;
}
I Googled but didn't found that much..
Z is a timezone, in your date format you have escaped it: 'Z'. Just try with following date format:
"yyyy-MM-dd'T'HH:mm:ssZ"
Related
I am suffering error from this code
java.text.ParseException: Unparseable date: "1998-09-17T00:00:00.000+08:00" (at offset 23)
I not sure what wrong with the code
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
Date date = null;
try
{
date = sdf.parse(startdate);
}
catch(Exception ex)
{
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
String dateStr = formatter.format(date);
System.out.println(dateStr);
What should I do?
The timezone format is not correct.
The Timezone should be +0800 not +08:00.
According to the samples in the Javadocs for SimpleDateFormat, you should be fine with
"yyyy-MM-dd'T'HH:mm:ss.SSSXXX" -> 2001-07-04T12:08:56.235-07:00
XXX is the ISO Format, that will allow the colon in the timezone.
In my android app I have
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
try{
date = formatter.parse("11/10/2013 08:50:13");
}
catch (ParseException e) {
e.printStackTrace();
}
Why I got ??
java.text.ParseException: Unparseable date: "11/10/2013 08:50:13"
you have two spaces between yyyy and HH
you have an extra space between yyyy and HH
I am trying to format the date string to Date, and then get the month/day from it:
String strDate="2013-05-15T10:00:00-07:00";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss-z");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday= sdfmonth.format(convertedDate);
but it returns me current month/day i.e 5/18. Whats wrong?
3 things :
There is a mistake in your format : 2013-05-15T10:00:00-07:00 has no meaning, it should be 2013-05-15T10:00:00-0700 (with no colon at the end, this is a timezone defined in RFC 822. (Look at the docs about Z).
Change your format to yyyy-MM-dd'T'HH:mm:ssZ as mentionned by #blackbelt
you get a bad date because you re-format your date whatever happens during parsing. Re-format in your try block, if and only if parsing worked.
----------Update
String strDate = "2013-05-15T10:00:00-0700";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
SimpleDateFormat sdfmonth = new SimpleDateFormat("MM/dd");
String monthday = sdfmonth.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
I don't know what is wrong in your code. for me it throws Unparseable exception like this.
java.text.ParseException: Unparseable date: "2013-05-15T10:00:00-07:00"
But the following way works well.
String strDate="January 2, 2010";
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
Date date = dateFormat.parse(strDate);
System.out.println(date.getMonth());
But in java Date is deprecated as per http://docs.oracle.com/ . Try to use Calender instead of Date.
I hope this will help you.
Any ideas why the followng fails on Android 2.2...
java.text.ParseException: Unparseable date: 2011-02-16 11:38:03.328 UTC
...while it works fine on a sun JRE 1.6 ?
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z");
try
{
Date date = dateFormat.parse("2011-02-16 11:38:03.328 UTC");
}
catch (ParseException e)
{
e.printStackTrace();
}
As mentioned ion Comment, can make the test even simpler:
new SimpleDateFormat("z").parse("UTC")
This throws a parse exception. I am using a nexus one device, android 2.2
Use this instead
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
I have a workaround now:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S 'UTC'");
try
{
Date date = dateFormat.parse("2011-02-16 11:38:03.328 UTC");
}
catch (ParseException e)
{
e.printStackTrace();
}
This allows me to at least parse the date in Android 2.2. My application has been modified to try "yyyy-MM-dd HH:mm:ss.S 'UTC'" and then try "yyyy-MM-dd HH:mm:ss.S z" if the first parse fails
Try specifying the locale like this:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z", Locale.ENGLISH);
I have copied this code and got casting exception...
try this code of mine
java.text.SimpleDateFormat format = new SimpleDateFormat(
"dd-MM-yyyy");
java.util.Calendar cal = Calendar.getInstance(new SimpleTimeZone(0,
"GMT"));
format.setCalendar(cal);
java.util.Date date = null;
try {
date = format.parse(EditProfile.dateOFBirth);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i'm getting this exception when i try to parse a date that i get from a SQL database, the date is a datetime on the sql and i recive it as a String with JSON, and seeing the excepcion i think that the string i recived is like this:2010-12-10 17:18:3600
this is the exception i get:
java.text.ParseException: Unparseable date: 2010-12-10 17:18:3600
i use the next code to parse the date (i get it from google) but it gets the exception
How can i modify this code to get the parse working???? please guive me the answer with code, date parsing and using of simpledateformat it's very hard for me
public void setPositiontime(String positiondate)
{
SimpleDateFormat FORMATTER = new SimpleDateFormat("d MMM yyyy HH:mm");
// pad the date if necessary
while (!positiondate.endsWith("00")){
positiondate += "0";
}
try {
this.positiondate = FORMATTER.parse(positiondate.trim());
} catch (ParseException e) {
throw new RuntimeException(e);
}
i get the error on this line: this.positiondate = FORMATTER.parse(positiondate.trim());
Try this
SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:SSSS");
It is simply a representation of what each digit in the format means so its:
Years-months-date hour:min:milli