Why does SimpleDateFormat fail to parse weekday in date string? - java

Running this program:
import java.text.*;
import java.util.*;
public class xx {
public static void main(String[] args) throws Exception {
final SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
format.setLenient(false);
format.parse("Tue, 16 Jan 2010 04:41:09 -0000");
}
}
Gives this result (java version "1.7.0_17"):
Exception in thread "main" java.text.ParseException: Unparseable date: "Tue, 16 Jan 2010 04:41:09 -0000"
at java.text.DateFormat.parse(DateFormat.java:357)
at xx.main(xx.java:7)
It appears that when set to non-lenient mode, the Tue, prefix is what fails to parse.
Question is, why does EEE, fail to match the Tue, prefix of the date string?

That's because January 16th wasn't a Tuesday, it was a Saturday.
public static void main(String args[]) throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
format.setLenient(false);
format.parse("Sat, 16 Jan 2010 04:41:09 -0000");
}
works just fine.

Related

how to parse a formatted string date to date object in java [duplicate]

This question already has answers here:
Parse any date in Java
(6 answers)
Closed 4 years ago.
I was trying to parse the formatted string date, was getting parse error
input date is "Wed Nov 11 14:24:46 IST 2015", need output date as "Wed Nov 11 2015 14:24:46 IST"
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat {
public static void main(String[] args) {
try {
String target = "Wed Nov 11 14:24:46 IST 2015";
SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
Date result = df.parse(target);
SimpleDateFormat df2 = new SimpleDateFormat("EEE MMM dd yyyy kk:mm:ss zzz");
String result2 = df2.format(result);
Date result3 = df.parse(result2);
System.out.println(result2);
System.out.println(result3);
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}
getting error as java.text.ParseException: Unparseable date: "Wed Nov 11 2015 14:24:46 IST"
I have updated my answer to do the parsing as mentioned in your question/comment. See below the explanation:
"Wed Nov 11 14:24:46 IST 2015"
to the following
"Wed Nov 11 2015 14:24:46 IST"
I setup two SimpleDateFormat objects as follow
SimpleDateFormat sourceFormat, destinationFormat;
//this is to format the string to Date object
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
//this is to format the Date object to desired pattern
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);
I then set the timezone as follow
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);
I use the sourceFormat object to format the date string to Date object as follow:
Date sourceDate = sourceFormat.parse(target);
//output: Wed Nov 11 08:54:46 GMT 2015
I then use the destination format to format the date object that represents the string as follow:
Date destinationDate = destinationFormat.format(d);
//output: Wed Nov 11 2015 14:24:46 IST
Basically in order to get a legit Date object I have to use the first SimpleDateFormat sourceFormat which contains pattern that maps the date in String. Once a legit Date object is created using the String then I use the second formatter to re-format the Date object. Below is full code that should give output if copy/pasted.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class ParseDate {
public static void main(String[] args) {
try {
String target = "Wed Nov 11 14:24:46 IST 2015";
SimpleDateFormat sourceFormat, destinationFormat;
sourceFormat = new SimpleDateFormat("EEE MMM d kk:mm:ss zzz yyyy", Locale.US);
destinationFormat = new SimpleDateFormat("EEE MMM d yyyy kk:mm:ss zzz", Locale.US);
TimeZone istTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
sourceFormat.setTimeZone(istTimeZone);
destinationFormat.setTimeZone(istTimeZone);
Date d = sourceFormat.parse(target);
System.out.println(d.toString());
//output: Wed Nov 11 08:54:46 GMT 2015
System.out.println(destinationFormat.format(d));
//output: Wed Nov 11 2015 14:24:46 IST
} catch (ParseException pe) {
pe.printStackTrace();
}
}
}

java.lang.IllegalArgumentException in Date java

Getting error
java.lang.IllegalArgumentException at java.util.Date.parse(Unknown Source) at java.util.Date.(Unknown Source)
Here is my java code
import java.util.Date;
public class DateCheck {
public static void main(String[] args) {
String dDate="Sat Apr 11 12:16:44 IST 2015";
Date cDate=null;
cDate = new Date(dDate);
}
}
I am using java 1.6
You have to use the method parse() of an implementation class of DateFormat.
The simplest way is using SimpleDateFormat.
String dDate="Sat Apr 11 12:16:44 IST 2015";
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date cDate = df.parse(dDate);
Try this code:
String dDate="Sat Apr 11 12:16:44 IST 2015"
DateFormat formatter = new SimpleDateFormat("d-MMM-yyyy,HH:mm:ss aaa");
Date date = formatter.parse(dDate);
System.out.println(date);

java does not parse for 'M dd, yyyy' date format

I want to parse date strings like "February 7, 2011" using "M dd, yyyy" format. But I get an exception.
Try this code. I ran it with two dates "November 20, 2012" and "January 4, 1957" and got this output:
arg: November 20, 2012 date: Tue Nov 20 00:00:00 EST 2012
arg: January 4, 1957 date: Fri Jan 04 00:00:00 EST 1957
It works fine. Your regex was wrong.
package cruft;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* DateValidator
* #author Michael
* #since 12/24/10
*/
public class DateValidator {
private static final DateFormat DEFAULT_FORMATTER;
static {
DEFAULT_FORMATTER = new SimpleDateFormat("MMM dd, yyyy");
DEFAULT_FORMATTER.setLenient(false);
}
public static void main(String[] args) {
for (String dateString : args) {
try {
System.out.println("arg: " + dateString + " date: " + convertDateString(dateString));
} catch (ParseException e) {
System.out.println("could not parse " + dateString);
}
}
}
public static Date convertDateString(String dateString) throws ParseException {
return DEFAULT_FORMATTER.parse(dateString);
}
}
Your parsing string is not correct as mentioned by others
To correctly parse February you need to use an english Locale or it may fail if your default Locale is not in English
DateFormat df = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);
Date dt = df.parse("February 7, 2011");
You will want to use "MMM dd, yyyy"
SimpleDateFormat("MMM dd, yyyy").parse("February 7, 2011")
See SimpleDateFormat
Assuming you are using SimpleDateFormat, the month format is incorrect, it should be MMM dd, yyyy
MMM will match the long text format of the month:
String str = "February 7, 2011";
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
Date date = format.parse(str);

How to parse "Thu Aug 04 00:00:00 IST 2011" to "04-08-2011"?

I want to parse the date Thu Aug 04 00:00:00 IST 2011 to dd-MM-YY format like 04-08-2011. How to do this in Java?
Use the following format to parse: EEE MMM dd HH:mm:ss z yyyy with SimpleDateFormat.parse(..)
The use another SimpleDateFormat with the dd-MM-yy format, to format(..) the resultant date. Something like:
SimpleDateFormat parseFormat =
new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = parseFormat.parse(dateString);
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
String result = format.format(date);
I hope you can find the solution from the sample code below
import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample
{
public static void main(String args[])
{
Date now=new Date();
System.out.println("dd/mm/yy format:" +DateFormat.getDateInstance(DateFormat.SHORT).format(now));
}
}
Now you will get your required format....

SimpleDateFormatter won't parse!

Hello I am trying to use the SimpleDateFormatter to parse the date Wed, 30 Jun 2010 15:07:06 CST
I am using the following code
public static SimpleDateFormat postedformat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date newDate = new Date(posteformat.parse("Wed, 30 Jun 2010 15:07:06 CST"));
but I am getting an illegalArgumentException. Please help!
postedformat.parse() returns a Date, and there is no Date(Date) constructor.
Presumably removing the call to new Date, so you say Date newDate = poste.... will suffice
Your code fragment doesn't compile. This slight modification compiles and parses successfully:
public static void main(String[] args) throws ParseException {
SimpleDateFormat postedformat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date newDate = postedformat.parse("Wed, 30 Jun 2010 15:07:06 CST");
System.out.println("newDate = " + newDate);
}
This is using Java 6 on Mac OS X.
There is no java.util.Date() constructor that takes a java.util.Date as an argument
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormat {
public static SimpleDateFormat postedformat =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
public static void main(String[] args) {
try {
Date newDate = postedformat.parse("Wed, 30 Jun 2010 15:07:06 CST");
System.out.println("Date: " + newDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Outputs:
Date: Wed Jun 30 22:07:06 BST 2010
The javadoc examples shows unescaped comma but for the US locale. So either try escaping the comma (as Aaron suggested) or use the other constructor and set the Locale:
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
Another problem could be the timezone ('CST') which is deprecated on the on hand and ambigious on the other (as per javadoc of java.util.TimeZone). Test, if it works without the timezone attribute (in both the format String and the value).

Categories