How to resolve Unparseable date error - java

I am receiving date from the RSS Feed in the below format
Fri Oct 23 11:07:08 IST 2015 which i am trying to convert it into
yyyy-MM-dd HH:mm format .
I have tried this way
public class ConvertDate {
public static void main(String args[]) throws ParseException
{
String passedate = "Fri Oct 23 11:07:08 IST 2015";
String res= convertdate(passedate);
System.out.println(res);
}
public static String convertdate(String recivieddate) throws ParseException {
SimpleDateFormat in = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
Date date = in.parse(recivieddate);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String newdate = out.format(date);
return newdate;
}
}
But i am getting
Exception in thread "main" java.text.ParseException: Unparseable date: "Fri Oct 23 11:07:08 IST 2015"
at java.text.DateFormat.parse(Unknown Source)
at ConvertDate.convertdate(ConvertDate.java:20)
at ConvertDate.main(ConvertDate.java:12)
Could you please let em know how to resolve this

The date pattern does not match the input. Try change the line
SimpleDateFormat in = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
to
SimpleDateFormat in = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");
Hope that helps

Your date has the format EEE MMM dd HH:mm:ss zzz yyyy with an english local.
This parses the date correctly:
new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH)
.parse("Fri Oct 23 11:07:08 IST 2015");

Try this:
SimpleDateFormat in = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
in.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta")); //or Asia/Jerusalem
String s2 = "Fri Oct 23 11:07:08 IST 2015";
Date date = in.parse(s2);
SimpleDateFormat out = new SimpleDateFormat("yyyy-MM-dd HH:mm");
out.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
System.out.println(out.format(date));
Output:
2015-10-23 11:07
Also, note the setTimeZone. IST can either stand for Indian ST or Israel ST so it would be better if you specify which time zone you really want.
Check here for IST ambiguity.

First check your actual date which you need to work .. In my case
String day="date:10/01/2018";(In selenium need to get it from web page so i got the above string from page)
SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy");
Date ndate = df.parse(day);
Calendar cal = Calendar.getInstance();
cal.setTime(ndate);
cal.add(Calendar.DATE, 1);
Date DDueDate1= cal.getTime();
day =df.format(DDueDate1);
When am working on this i got unparsable error....
So the day string contains some part of characters . So just remove those characters from string by using day.split(":"); String day1=day[1];
just give this day1 string in parse(); Now the updated code like as below..
SimpleDateFormat df=new SimpleDateFormat("MM/dd/yyyy");
Date ndate = df.parse(day1);
Calendar cal = Calendar.getInstance();
cal.setTime(ndate);
cal.add(Calendar.DATE, 1);
Date DDueDate1= cal.getTime();
day =df.format(DDueDate1);

Related

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

Unparseable date: when convert date from dd MMM yyyy format to dd/MM/yyyy

I get Unparseable error when I run below code. How can I convert dd MMM yyyy format to dd/MM/yyyy format?
public Calendar myMethod(){
String dateStr = "16 Dec 2014"
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date thedate = formatter.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(thedate);
return cal;
}
Assuming you fix the blatant syntax errors, then:
String dateStr = "16 Dec 2014" // <== This date is in dd MMM yyyy
formatter = new SimpleDateFormat(dd/MM/yyyy); // <== This SDF is in dd/MM/yyyy
Date thedate = formatter.parse(dateStr); // <== So how can it be expected to parse?
What you do is create a parser for the format you need to parse, and a formatter for the format you want:
String dateStr = "16 Dec 2014";
SimpleDateFormat parser = new SimpleDateFormat("dd MMM yyyy");
Date thedate = parser.parse(dateStr);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateStrReformatted = formatter.format(thedate);

Date formatting is not formatting correctly

I can't figure out why this is returning
Wed Jul 02 18:21:27 CDT 2014
instead of
07/02/14 6:21 pm
pubdate = Mon, 30 Jun 2014 22:37:15 +0000
public void setPubDate(String pubDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
long x = dateFormat.parse(pubDate).getTime();
Date date = new Date(x);
SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
newFormat.format(dateFormat.parse(pubDate));
this.pubDate = date;
}
If you want to print the format you want, you have to use String to represent your date, otherwise, Date type will always print this format "dow mon dd hh:mm:ss zzz yyyy"
this.pubDate = date;//Assign the reference of date Object
//this.pubDate will have value of date NOT Format :)
But here format won't be passed to pubDate as that will remain as it is.
If you want to make your pubDate to have dd/Mm/yyyy aa format you have to format the pubDate as well here you are only assigning reference from one date to other but formation on one date won't affect the other one you have to apply that to this.pubDate whenever you want to use pubDate.
You can declare general format(Class level Object) and use it in your program whenever you want to display the date.
Because Date has toString() which per the Javadoc,
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
where:
dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
dd is the day of the month (01 through 31), as two decimal digits.
hh is the hour of the day (00 through 23), as two decimal digits.
mm is the minute within the hour (00 through 59), as two decimal digits.
ss is the second within the minute (00 through 61, as two decimal digits.
zzz is the time zone (and may reflect daylight saving time). Standard time zone
abbreviations include those recognized by the method parse. If time zone
information is not available, then zzz is empty - that is, it consists of no
characters at all.
yyyy is the year, as four decimal digits.
When you want to deviate from that, you will need your newFormat -
// As a String
System.out.println(newFormat.format(dateFormat.parse(pubDate)));
public void setPubDate(String pubDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
long x = dateFormat.parse(pubDate).getTime();
Date date = new Date(x);
SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
return newFormat.format(dateFormat.parse(pubDate));
}
Use corrected code below:
public void setPubDate(String pubDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
long x = dateFormat.parse(pubDate).getTime();
Date date = new Date(x);
SimpleDateFormat newFormat = new SimpleDateFormat("MM/dd/yy H:mm aa");
System.out.println("Formatted date is ="+ newFormat.format(x));
}
Try this, Create your custom date class
public class MyDate extends Date
{
#Override
public String toString() {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy hh:mm aa");
return dateFormat.format(new Date());
}
}
then print the object like
System.out.println(new MyDate());

Parsing String with spaces to Date, lead to ParseException

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

Timezone Date parsing

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.

Categories

Resources