I'm receiving an exception when parsing the following date:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date parsedDate=null;
try {
parsedDate=sdf.parse("Thu Jan 26 15:05:48 COT 2012");
} catch (ParseException e) {
e.printStackTrace();
}
Is the pattern incorrect? What would be the correct form to parse the date string?+
The default SimpleDateFormat constructor doesn't support all Locales.
You would have to specify the Locale:
SimpleDateFormat sdf = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date parsedDate = null;
try {
parsedDate = sdf.parse("Thu Jan 26 15:05:48 COT 2012");
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Try using only one z:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Have you ever tried a single 'z' instead of a triple 'z' ?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Should become
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Perhaps you need to use CO instead of COT for the timezone?
Related
I'm new in Android and I have a problem with this. I want to convert this datetime string "EEE MMM dd HH:mm:ss Z yyyy" to "HH:mm". Here is my string:
String date = "Mon Sep 28 19:49:26 GMT+07:00 2015";
I have tried to convert it like this but it failed and I couldn't see what exception was because Exception e always null!
private String convertDate(String date) {
try {
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
Date d = format.parse(date);
SimpleDateFormat serverFormat = new SimpleDateFormat("HH:mm");
return serverFormat.format(d);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
So how can I get "HH:mm" or how can I convert it? Does any problem in my convert function?
Locale
Set your Locale to Locale.ENGLISH.
... = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
... = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
I want get Date from following Json file
which has string:
{"Notice":[{"nid":1,"title":"Greeting","notice":"hello","priority":"","date":"Jan 30, 2015 11:35:29 AM","userid":"1"}]}
I want to parse it using following code:
java.util.Date temp = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy").parse(object.getString("date"));
d.setDate(temp);
but it gives me Exception:
java.text.ParseException: Unparseable date: "Jan 30, 2015 11:35:29 AM" (at offset 0)
if any suggestion always welcome.
Change the date format to : MMM dd, yyyy HH:mm:ss a to parse the date.
java.util.Date temp = SimpleDateFormat("MMM dd, yyyy HH:mm:ss a").parse(object.getString("date"));
You have to add the Locale when you create the SimpleDateFormat:
java.util.Date temp = new SimpleDateFormat("EEE MMM d',' yyyy HH:mm:ss a", Locale.UK).parse(object.getString("date"));
And use the correct format.
String dtStart = "Jan 30, 2015 11:35:29 AM";
SimpleDateFormat format = new SimpleDateFormat("MMM d, yyyy HH:mm:ss zzz ",Locale.getDefault);
try {
Date date = format.parse(dtStart);
System.out.println(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am trying to convert String to Date to compare from current Date and it throws parse Exception
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
try {
java.util.Date cookiedate = format.parse("Tue Apr 29 11:40:55 GMT+04:00 2014");
Calendar currentDate = Calendar.getInstance();
String dateNow = format.format(currentDate.getTime());
java.util.Date currDate = format.parse(dateNow);
if (currDate.getTime() > cookiedate.getTime()) {
return true;
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In your format, "yyyy-MM-dd HH:mm:ss" will match date string like "2013-03-30 15:57:00", so you get a ParseException.
If you want to parse "Tue Apr 29 11:40:55 GMT+04:00 2014", you should use "EEE MMM dd HH:mm:ss z yyyy" Change your code to
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
At first, you should parse the string date to Date object using EEE MMM dd HH:mm:ss Z yyyy format then convert that Date to yyyy-MM-dd HH:mm:ss format as follows...
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
try {
Date cookiedate = format.parse("Tue Apr 29 11:40:55 GMT+04:00 2014");
Calendar currentDate = Calendar.getInstance();
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
cookiedate = format.parse(format.parse(cookiedate));
String dateNow = format.format(currentDate.getTime());
Date currDate = format.parse(dateNow);
if (currDate.getTime() > cookiedate.getTime()) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
I think it is pretty obvious that formatted date "Tue Apr 29 11:40:55 GMT+04:00 2014" does not match format "yyyy-MM-dd HH:mm:ss".
If you are using format "yyyy-MM-dd HH:mm:ss" you should parse strings like 2014-03-30 10"59:23.
If you want to parse string like "Tue Apr 29 11:40:55 GMT+04:00 2014" you should use format like EEE MMM dd HH:mm:ss z yyyy. (I am not sure about z, probably it should be Z).
How do I convert date string 'Fri Mar 14 09:44:31 IST 2014' to millisec?
I tried with this Java code:
String dateStr = "Fri Mar 14 09:44:31 IST 2014";
SimpleDateFormat sdf = new SimpleDateFormat("MM dd HH:MM:ss");
try {
System.out.println(sdf.parse(dateStr).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
your SimpleDateFormat is not correct.
Try this
SimpleDateFormat sd = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try following code.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
What's wrong with below date format? I'm getting java.text.ParseException: Unparseable date Thu, 03 May 2012 14:00:00 CEST
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
Date parsedDate = DATE_FORMATTER.parse(inputDate);
I have tried below combinations but am out of luck:
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz", Locale.US);
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
I'm getting the same exception if modify CEST to CET, but not for PST. Any pointer will be appreciated. Thanks.
It works for me too
package test.java;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
Date parsedDate = DATE_FORMATTER.parse(inputDate);
System.out.println("Date = " + parsedDate);
}
}
And the output I get is:
Date = Thu May 03 13:00:00 BST 2012
Its working... see this..
- Please correct the typo from inputDateString to inputDate in parse()
Its a Working Java Code... Modify it to suit your Android use....
public class CopyArray {
public static void main(String[] args) {
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z");
try {
Date parsedDate = DATE_FORMATTER.parse(inputDate);
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Aside from the minor issue that you are trying to parse inputDateString when your variable is actually called inputDate, I'm able to run your code with no exceptions.