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
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.
I am using the this code to convert date time to unixtime gmt . It is working fine on most of the devices but it is crashing in a few . I am unable to determine the cause. How can I fix it ?
int gmtOffset = TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings();
String dt="11-01-2016 5:8 AM";
DateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
try {
date = format.parse(dt);
System.out.println("Date ->" + date);
} catch (Exception e) {
e.printStackTrace();
}
long unixtime = (date.getTime() -(gmtOffset))/1000;
error:
Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference
When the locale is not set to US English, the locale's Am/pm marker may be different. For example, in Chinese, the AM/PM maker will be 上午/下午.
Thus, to force use of AM/PM, you'd need to use a US date format:
DateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a", Locale.US)
I ran this code when in the US locale, and it worked fine. When I switched to Chiense, I got the message:
java.text.ParseException: Unparseable date: "11-01-2016 5:8 AM" (at
offset 15)
Offset 15 is the AM/PM marker. Thus, when exiting the try/catch block, the date object will remain null, causing the NullPointerException when calling the getTime() method.
By the way, you might consider using Joda DateTime to get the Unix epoch, something along the lines of: Joda DateTime to Unix DateTime.
Maybe the dtStart doesn't have the same format always (in this case it is hardcoded). If the format of dtStart is not correct after try catch block "date" is null and the app crashes at the last line because the date is null. (date.getTime())
The exception is throw when date is null.
And date will be null if dtStart can't be parsed.
Try like this:
int gmtOffset = TimeZone.getDefault().getRawOffset() + TimeZone.getDefault().getDSTSavings();
String dtStart="11-01-2016 5:8 AM";
DateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = null;
long unixtime = 0;
try {
date = format.parse(dtStart);
System.out.println("Date ->" + date);
unixtime = (date.getTime() -(gmtOffset))/1000;
} catch (Exception e) {
e.printStackTrace();
}
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"
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.
I have a date in 02/21/2013 04:52:10 PM this format.
How do I convert it into MM-dd-yyyy HH:mm. I already tried few things but it keeps throwing error
java.text.ParseException: Unparseable date: "02/21/2013 04:52:10 PM" (at offset 2)
I really need a help from date format expert.
Thanks
Try this one:
DateFormat inputFormat = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss a", Locale.getDefault());
DateFormat outputFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm", Locale.getDefault());
try {
System.out.println("Converted: " + outputFormat.format(inputFormat.parse("02/21/2013 04:52:10 PM")));
} catch (ParseException e) {
e.printStackTrace();
}
Output: 01-21-2013 16:52
Checkout http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html for more
Cheers
Construct two SimpleDateFormat objects. The first you parse() the value from into a Date object, the second you use to turn the Date object back into a string, e.g.
try {
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateFormat df2 = new SimpleDateFormat("dd-MMM-yyyy");
return df2.format(df1.parse(input));
}
catch (ParseException e) {
return null;
}
Parsing can throw a ParseException so you would need to catch and handle that.
In addition, check this out:-
http://developer.android.com/reference/java/text/SimpleDateFormat.html
and more:-
http://msdn.microsoft.com/en-us/library/aa226054(SQL.80).aspx
and more:-
Date format conversion Android
You need verify if your String have / or - in the mask, case have / then you must used:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", LOCALE_BR);
Date convert = sdf.parse(data);
case your String have - then you must used:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", LOCALE_BR);
Date convert = sdf.parse(data);
Or then, use the replace() for change of the caracter - for /