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).
Related
I've been struggling a strange question.
I'm trying to parse a date format looks like this
"Thu, 18 Aug 2016 16:25:25 GMT"
to only Month and date like this
"08/18"
well I use two SimpleDateFormat, to parse and format the input and output
DateFormat inputFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
DateFormat outputFormat = new SimpleDateFormat("MM/dd");
however, everything seems to be correct when using genymotion emulator
but when it comes to the real phone, say sony Xperia Care
the following questions kept on comming
java.text.ParseException: Unparseable date: "Wed, 10 Aug 2016 15:40:57 GMT" (at offset 0)
so whats the reason that this happens?
Try to use
new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
Your String-DAte has information that is related to an specific language, therefor you need to prepare the SimpleDAteFormat for such an information.
ERGO: you need to give a Locale
Example:
public static void main(String[] args) throws ParseException {
String dateStr = "Thu, 18 Aug 2016 16:25:25 GMT";
DateFormat inputFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
Date d = null;
d = inputFormat.parse(dateStr);
DateFormat outputFormat = new SimpleDateFormat("MM/dd");
System.out.println("After format: " + outputFormat.format(d));
}
I am receiving a Java Date formatted like so: "Sun Sep 14 02:00:00 PDT 2014" into a yyyy-MM-dd format but I can't seem to parse it. What I tried is the following:
String time = "Sun Sep 14 02:00:00 PDT 2014";
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
Date contractEffectiveDateFormat = f.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);
However, I get an error saying that this date is unparsable. I'm not sure how to go about parsing this date because if I try to parse the date using the following:
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
which is how to actually parse the date correctly into a Date object,
the string would turn into a Date object, but I can't seem to do anything with it from there. I want to turn it in so that it looks like 2014-09-14. Any ideas on how to do so? Thanks!
Use two DateFormat(s) one for input and for output,
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat out = new SimpleDateFormat("yyyy-MM-dd");
DateFormat in = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
try {
Date effectiveDate = in.parse(time);
System.out.println("Date: " + out.format(effectiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
Output is the requested
Date: 2014-09-14
Your incoming string is this String time = "Sun Sep 14 02:00:00 PDT 2014";
which means the SimpleDateFormat pattern should match the incoming String pattern so you need to use SimpleDateFormat like this
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
Then when you called parse() on inFormat it will give you Date Object which doesnot have particular format associated with it. So in order to format the Date again you need to create SimpleDateFormat object specifying the format you want which is this
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Ultimately bind all together
One more thing always specify the Locale
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
// good practice to specify the locale
DateFormat inFormat=new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy",Locale.ENGLISH);
try {
Date parsedDate = inFormat.parse(time);
System.out.println("Required Formatted Date: " + outFormat.format(effectiveDate));
} catch (ParseException e) {
e.printStackTrace();
}
Simply add another SimpleDateFormat that'll allow you to present the Date object the way you want:
public static void main(String[] args) throws ParseException {
String time = "Sun Sep 14 02:00:00 PDT 2014";
DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");
Date contractEffectiveDateFormat = df.parse(time);
System.out.println("Date: " + contractEffectiveDateFormat);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(f.format(contractEffectiveDateFormat)); // prints 2014-09-14
}
I have to strings like this
Thu Oct 03 07:47:22 2013
Mon Jul 05 08:47:22 2013
I want to compare these dates, I am using SimpleDateFormat("EEE MMM dd HH:mm:ss yyy") but it gives me an exception : java.text.ParseException: Unparseable date:
Please help me to solve this problem!
You're missing an y for year:
EEE MMM dd HH:mm:ss yyyy
but you should use a more robust library, org.jodatime.
import org.joda.time.format.DateTimeFormat;
import org.joda.time.DateTime;
DateTimeFormat format = DateTimeFormat.forPattern("EEE MMM dd HH::mm:ss yyyy");
DateTime time = format.parseDateTime("Thu Oct 03 07:47:22 2013");
You missed a y in the format. 4 y were required for the year(though it may work fine with yyy, its better to use yyyy as it'll make your format more readable to others). And to get the DateTime object, you can use the Date object which you get by parsing the String to construct your DateTime.
Try something like this:-
String str = "Thu Oct 03 07:47:22 2013";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy"); // You missed a y here.
try {
Date d = sdf.parse(str);
DateTime dt = new DateTime(d.getTime()); // Your DateTime Object.
} catch (ParseException e) {
// Parse Exception
}
try with this method
public static Date formatStringToDate(String strDate) throws ModuleException {
Date dtReturn = null;
if (strDate != null && !strDate.equals("")) {
int date = Integer.parseInt(strDate.substring(0, 2));
int month = Integer.parseInt(strDate.substring(3, 5));
int year = Integer.parseInt(strDate.substring(6, 10));
Calendar validDate = new GregorianCalendar(year, month - 1, date);
dtReturn = new Date(validDate.getTime().getTime());
}
return dtReturn;
}
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);
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....