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).
Related
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();
}
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);
This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
I have a
String dateString = "Fri Feb 14 00:00:00 IST 2014";
I need output in Date datatype like 2014-02-14.
Here is the code which is throwing Parse exception.
Need help in this.
public static void main(String args[]){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date convertedDate = null;
try {
convertedDate = df.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
}
Base point is - Input string should match with date pattern
Raised parse exception as becasue wrong pattern, use this date pattern - EEE MMM dd hh:mm:ss z yyyy
DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
You have to convert dateString to matching Date format and then you can format that Date what ever the format you want.
Try this
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
DateFormat df2 = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date date=df2.parse(dateString); // convert stringDate to matching Date format
System.out.println(df1.format(date));
Out put:
2014-02-14 12:00:00
Try this:
public static void main(String[] args) {
DateFormat df = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
String dateString = "Fri Feb 14 00:00:00 IST 2014";
Date convertedDate = null;
try {
convertedDate = df.parse(dateString);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(convertedDate);
}
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 parsing a UTC Date string with SimpleDateFormat and it is parsed as the previous date.
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
This string "Fri, 06 Apr 2012 04:00:00 GMT" is being parsed as 4/5/2012. Why? Thanks.
Below is example of date conversion...
For your program, do changes accordingly and let me know what output you are getting.
String dateStr = "Thu Jan 19 2012 01:00 PM";
DateFormat readFormat = new SimpleDateFormat( "EEE MMM dd yyyy hh:mm aaa");
DateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse( dateStr );
} catch ( ParseException e ) {
e.printStackTrace();
}
String formattedDate = "";
if( date != null ) {
formattedDate = writeFormat.format( date );
}
System.out.println(formattedDate);
Output is 2012-01-19 13:00:00
Do the changes as per output you are expecting.
Beware of JDK 1.5.0_22 (linux). Produces incorrect date. Switched to JDK 1.6 problem gone.