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();
}
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);
How to convert date fromat from "dd MMM yyyy" to "yyyy-MM-dd"?
I know I have to use SimpleDatFormat but it doesn't work, neither does any solution from similar questions.
I have a date "18 Dec 2015" that I am trying to format but I get this
java.text.ParseException: Unparseable date: "18 Dec 2015"
Here's my code:
public String parseDate(String d) {
String result = null;
Date dateObject = null;
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy");
try {
dateObject = dateFormatter.parse(d);
dateFormatter.applyPattern("yyyy-MM-dd");
result = dateFormatter.format(dateObject);
} catch (ParseException e) {
System.out.println(e);
}
return result;
}
Did you try
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");
instead of
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM yyyy");
(note the hyphens, since your pattern doesn't match your input)
Also helpful: using Locale.US as recommended by #ZouZou
You are passing input as "18-Dec-2015" instead of the form "dd MMM yyyy". Try and pass input like 18 Dec 2015 and it should work.
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).
This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 9 years ago.
I am facing the problem while converting the date:
Current format is:Thu Sep 05 12:07:46 IST 2013(dow mon dd hh:mm:ss zzz yyyy)
I need to convert in to:09/04/2013 11:38 PM PDT(mm/dd/yyyy hh:mm a zzz)
But i am not able to convert.
Try using SimpleDateFormatter. You have to tell it the input/output format, you can do that based on this description (you can also find a few common examples there).
The code will be something like this:
try {
String input = "Thu Sep 05 12:07:46 IST 2013";
DateFormat formatter = new SimpleDateFormat("I leave this to you :-)))");
System.out.println(formatter.parse(input));
} catch (ParseException e) {
e.printStackTrace();
}
Hope that helps.
You can do this
TimeZone tz = TimeZone.getTimeZone("PST8PDT"); // example
// required format. Remember M is for month, m for miniute
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a zzz");
df.setTimeZone(tz);
String text = df.format(new Date());// current time
System.out.println(text);
Also please check this TimeZones in Java
You try to convert dateformat and timeZone as well, so you need to convert the timezone in your code.
SimpleDateFormat sf = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
isoFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = isoFormat.parse("mm/dd/yyyy hh:mm a zzz");
this may help you.
try {
DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");
String s = dfto.format(date);
System.out.println(s);
} catch (ParseException e) {
}
OutPut
09/05/2013 00:07:46 AM IST
update
try {
DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
dfto.setTimeZone(zone);
Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");
String s = dfto.format(date);
System.out.println(s);
} catch (ParseException e) {
}
output
09/04/2013 11:37:46 AM PDT
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?