I am using SimpleDateFormat and I am getting ParseException as shown below.
java.text.ParseException: Unparseable date: "Mon Jul 02 21:56:10 AST 2012"
Code I have have is
String dateStr = "Mon Jul 02 21:56:10 AST 2012";
DateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy ");
DateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = readFormat.parse(dateStr);
} catch (ParseException e) {
System.out.println("Error in parsing date ********");
}
String formattedDate = "";
if (date != null) {
formattedDate = writeFormat.format(date);
}
System.out.println("Formatted date is " + formattedDate);
Any idea where I am going wrong?
Update 1
I also tried with
DateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy ");
^
but still same exception.
Your code works (with z, and not Z), as soon as I specify that the date format should use the symbols of the English locale:
SimpleDateFormat readFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
readFormat.setDateFormatSymbols(DateFormatSymbols.getInstance(Locale.ENGLISH));
As per eran, you also have extra space after yyyy: yyyy "). Remove that extra space.
The format code Z is for timezone offset, like -0800, while the format code z is for the written format, such as PST or CST, according to what's described at SimpleDateFormat. Double-check that your parse pattern has the intended capitalization.
Related
Consider the snippet:
String dateStr = "Mon Jan 32 00:00:00 IST 2015"; // 32 Jan 2015
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
System.out.println(ddMMyyyy.format(formatter.parse(dateStr)));
gives me the output as
01.02.2015 // Ist February 2015
I wish to prevent this to make the user aware on the UI that is an invalid date?
Any suggestions?
The option setLenient() of your SimpleDateFormat is what you are looking for.
After you set isLenient to false, it will only accept correctly formatted dates anymore, and throw a ParseException in other cases.
String dateStr = "Mon Jan 32 00:00:00 IST 2015"; // 32 Jan 2015
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
formatter.setLenient(false);
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
try {
System.out.println(ddMMyyyy.format(formatter.parse(dateStr)));
} catch (ParseException e) {
// Your date is invalid
}
You can use DateFormat.setLenient(boolean) to (from the Javadoc) with strict parsing, inputs must match this object's format.
DateFormat ddMMyyyy = new SimpleDateFormat("dd.MM.yyyy");
ddMMyyyy.setLenient(false);
Set the date formatter not to be lenient...
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
formatter.setLenient(false);
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).
I want to convert the timestamp Wed, 20 Feb 2013 11:41:23 GMT to 2013-02-20T11:41:23Z . How can I do this? I want to ISO-8601 in UTC format(2013-20-02T04:51:03Z).
My code is below
Date date=new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String strDate = date.toString();
Date dt = formatter.parse(strDate );
System.out.println("Date " +dt);
Output is:
Exception in thread "main" java.text.ParseException: Unparseable date: "Wed Feb 20 03:50:03 PST 2013"
Its your format what is wrong, use:
"EEE MMM dd HH:mm:ss Z yyyy" that is what is comming out of the exception, in the question you have "EEE, dd MMM yyyy HH:mm:ss Z" for Wed, 20 Feb 2013 11:41:23 GMT
Try:
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss Z yyyy");
String strDate = date.toString();
Date dt = null;
try {
dt = formatter.parse("Wed Feb 20 03:50:03 PST 2013");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("Date " + dt);
System.out.println(new Timestamp(new Date().getTime()));
Consider using Joda Time, which has built in support for parsing and outputting ISO-format date strings.
new DateTime(DateTimeZone.UTC).toString()
You need 2 DateFormatters, one for parsing and one for output. You have the one for output.
EDIT: Output works like this:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String output = formatter.format(new Date());
System.out.println("Date " + output);
You already have a Date object. You just need to format it to String.
formatter.format(date) should give you the desired result if the pattern in the SimpleDateFormat constructor is valid.
With the above implementation your code looks like this:
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String dt = formatter.format(date);
System.out.println("Date " + dt);
Which results in an output like - Date 2013-02-20T17:39:45Z.
DateFormat format = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'");
format.setTimeZone (TimeZone.getTimeZone ("UTC"));
Date date = new Date ();
System.out.println ("Date is: " + format.format (date));
I have date strings in this form Thu Aug 02 00:00:00 GMT+00:00 2012
I have tried to use this method to parse these String in a Date object
public Date fromStringToDate(String data) {
Date result;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
try {
result = sdf.parse(data);
return result;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
But doesn't works and I get this error
java.text.ParseException: Unparseable date: "Thu Aug 02 00:00:00 GMT+00:00 2012"
I suppose that the problem is caused by a wrong SimpleDateFormat, but I don't know the right syntax to fix it.
You need to adjust the date format to the given string:
EEE MMM dd HH:mm:ss Z yyyy
Make sure use the correct placeholders, case sensitive, etc. Take a look to the Date and Time Patterns.
Sorry, I had a mistake with the 'z' pattern, 'Z' is:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Take a look to Locale.US, it is important to apply because the months and and days are in english.
Use this date formatting:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy")
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.