How do I parse this date format? - java

How do I parse this date format:
"Tue Dec 16 07:01:31 CET 2014"
I tried the following:
DateFormat dateformat = DateFormat.getDateTimeInstance(DateFormat.FULL,
DateFormat.FULL, Locale.CANADA);
But that doesn't seem to be correct. Do you recognize this format and know how I need to configure DateFormat?

Try this;
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
Date d = parserSDF.parse("Tue Dec 16 07:01:31 CET 2014");

Related

Cannot parse a json string to date

I am trying to convert the following json string: "Mon Apr 04 00:00:00 CEST 2016" to a new date object by a simpleDateFormat. But i dont see why it wont work hope some one can help me.
String date = "Mon Apr 04 00:00:00 CEST 2016";
I get the following error:
(java.text.ParseException) java.text.ParseException: Unparseable
date: "Mon Apr 04 00:00:00 CEST 2016"
public Date parseDate(String date)
{
try
{
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date returnDate = formatter.parse(date);
return returnDate;
}
catch (ParseException e)
{
e.printStackTrace();
return null;
}
}
you need to parse with the locale:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
"EEE MMM ddHH:mm:ss z yyyy"
looks you forgot to put the space after dd:
"EEE MMM dd HH:mm:ss z yyyy"

Prevent invalid date from getting converted into date of next month in jdk6?

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);

Is this a Valid DateFormat in Java

I want to parse this date "Mon Mar 09 2015 00:00:00 GMT+0530 (India Standard Time)" in java SimpleDateFormat class as "MM-dd-yyyy", is this a valid date format? If yes, how can I do that?
String inputDate = "Wed Sep 17 2014 00:00:00 GMT+0530 (India Standard Time)";
//SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss"); -- Not working
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss z"); // Not Working
Date date = sdf.parse(inputDate);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
Tried following piece of code:
String inputDate = "Wed Sep 17 2014 00:00:00 GMT+0530 (India Standard Time)";
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm:ss 'GMT'z");
Date date = sdf.parse(inputDate);
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(date));
Output:
2014-09-17
Try by using this format "E MMM dd yyyy HH:mm:ss z" with SimpleDateFormat class.

Convert EEE MMM dd HH:mm:ss ZZZ yyyy to YYYY-MM-dd JAVA

I want to convert : Thu Feb 02 00:00:00 WET 2012 to 02/02/2012 (with date type not string) using JAVA.
I did
String date = "Thu Feb 02 00:00:00 WET 2012";
SimpleDateFormat formatnow = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);
SimpleDateFormat formatneeded=new SimpleDateFormat("YYYY-MM-dd");
Date date1 = formatnow.parse(date);
String date2 = formatneeded.format(date1);
Date date3= formatneeded.parse(date2);
System.out.println(date3);
And I'm having : Thu Feb 02 00:00:00 WET 2012.
Can anyone tell me where is the problem ??
The Date object does not hold any information about the display format you want. So parsing a date from a formatted date string is not going to 'remember' any formatting.
System.out.println(date3) will print value of date3 using java's toString method.
You have the formatted date string in date2. So System.out.println(date2) should give you the right value.

Parsing string to date

For a Android app I need to pase a string to a date object. This is the code I use:
SimpleDateFormat df = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.ENGLISH);
Date d = df.parse("Thu May 15 00:00:00 CEST 2008");
But I get the following exception: java.text.ParseException: Unparseable date: "Thu May 15 00:00:00 CEST 2008"
Who can help me?
What happens when you SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"); without locale info?
This other question/answer may help you as well:
SimpleDateFormat and locale based format string

Categories

Resources