I'm trying to parse date from string to Date
My string date is: Fri Apr 30 01:20:29 +0700 2010
My code is:
SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = format.parse(input);
But i'm getting an Unparsable date exception.
What's wrong?
Check your format, you only have one E instead of three :
EEE MMM dd HH:mm:ss Z yyyy
EDIT : also check your JVM's language locale or specify one for your call.
As writen in comments, following code works :
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Related
I am using shannah´s Data Access Library to access my objects via his DAO interfaces.
I face a very strange behaviour with parsing the date values when the unmap method gets called once my stuff is in the database. It only fails on CEST (Central European Summer Time)
I tried to use the NumberUtil.dateValue Method to parse it but it still fails...
java.lang.RuntimeException: Failed to parse string date format Thu Mar 31 00:00:00 CEST 2016. Could not find appropriate format parser.
i defined DateFormat´s as these
dateFormats[0] = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
dateFormats[1] = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
dateFormats[2] = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZ yyyy");
dateFormats[3] = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
also tried to trim the String but it didnt help.
Im out of ideas as SDF doesnt have the constructor as the normal JDK with the 2nd parameter beeing the Locale.
// Method that take date time string , input pattern and out put pattern,that return formatted date as string
public String parseDate(String dateTime,String inputPattern, String outputPattern) throws ParseException {
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern, Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = inputFormat.parse(dateTime);
String str = outputFormat.format(date);
return str;
}
// call method
String date = parseDate("Thu Mar 31 00:00:00 CEST 2016" ,"EEE MMM dd HH:mm:ss zzz yyyy", "MM-dd-yyyy:HH:mm:ss");
// print date
System.out.println(date);
// Result
03-31-2016:03:00:00
I now did a workaround to this issue, i can still develop my api and stuff further but to be honest i dont like this fix, as i think it will make some problems with the TimeZones later.
Can someone with more experience tell me anything about this fix, if it may cause errors with TimesZones and Summer/Winter times?
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");
String dateValue = plannedDate;
String year = dateValue.substring(dateValue.length() - 4);
dateValue = dateValue.substring(0, 20);
dateValue += year;
this.plannedDate = sdf.parse(dateValue);
Any advice or possible fixes are welcome (y)
I have the following variable declaring format of SimpleDateFormat:
private static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
When I try to parse following string with above format I get Unparsable date exception :
newDate = dateFormat.parse("Mon Mar 24 16:49:31 UTC 2013");
What am I doing wrong? Any idea? It is strange that above line works in emulator, but not on phone.
I want to convert a Timestamp value which is passed as String to SimpleDateFormat Object into Time Value but it throws a Unparseable date exception.
The Value which i am passing is Thu Jan 1 17:45:00 UTC+0530 1970
Bur i am getting an Exception as mentioned below:
java.text.ParseException: Unparseable date: "Thu Jan 1 17:45:00 UTC+0530 1970"
Please find the below code which i have implemented(Not Working):
static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
static SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
static SimpleDateFormat outputFormatTime = new SimpleDateFormat("HH:mm:ss");
public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
{
Date parsedDate = dateFormat.parse(utcDateValue);
String returnDate=outputFormatTime.format(inputFormat.parse(parsedDate.toString()));
return returnDate;
}
If i use the below code it works fine for me(Working) but its a depreciated function of Date which i want to avoid..
#SuppressWarnings("deprecation")
public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
{
Date dateValue=new Date(utcDateValue);
Date parsedDate = dateFormat.parse(dateValue.toString());
String returnDate=outputFormatTime.format(inputFormat.parse(parsedDate.toString()));
return returnDate;
}
Please Guide Me To implement the logic where i have missed. Thanks in advance.
with an addition to the answers if the formatting string is like this
"EEE MMM dd HH:mm:ss z yyyy"
then your input string should be
"Thu Jan 1 17:45:00 +0530 1970"
note that the "UTC" is skipped as implicitly it refers to the RFC 822 time zone
First of all, your 2nd SimpleDateFormat object, is not needed at all. You are doing the extra work, which is not needed. So, remove this variable:
static SimpleDateFormat inputFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US); // Not needed.
Secondly DateFormat#format(Date) methods takes a Date object. You are passing it a String. That wouldn't work. That is why you don't need the above object. There is no need to do a inputFormat.parse(parsedDate.toString()) again.
Now, the format to parse your current string should be:
"EEE MMM dd HH:mm:ss 'UTC'z yyyy"
You need to give the UTC in quotes, before z. Or for more general case:
"EEE MMM dd HH:mm:ss zZ yyyy"
So, your code should be like:
static SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.US);
static SimpleDateFormat outputFormatTime = new SimpleDateFormat("HH:mm:ss");
public static String convertUtcDateStringToTime(String utcDateValue) throws Exception
{
Date parsedDate = dateFormat.parse(utcDateValue);
String returnDate=outputFormatTime.format(inputFormat);
return returnDate;
}
You input dateformat needs to be
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.US);
The other formatting is all upto, you based on your requirements.
This question already has answers here:
how to parse output of new Date().toString()
(4 answers)
Closed 3 years ago.
I have a String containing the result of toString() called on an instance of java.util.Date. How can I parse this value back to a Date object?
The Java docs say that toString() converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
but of course there is no such format tag as "dow" or "mon".
Could someone please help me with this problem. Please note that unfortunately the toString() call is in a piece of code out of my control.
If you don't have control over the code that's generating the String:
To parse the toString() format you need to set the SimpleDateFormat locale to english and use the format: "EEE MMM dd HH:mm:ss Z yyyy".
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", new Locale("us"));`
I didn't test but something like this probably would work:
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM HH:mm:ss z yyyy");
Date date = sdf.parse(dateStr);
If not, try to correct it using documentation:
http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#toString()
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
First take a look at all the date formats provided by java Date Formats.
And you can use SimpleDateFormat class to do what you want.
public class DateFormatTest
{
public DateFormatTest()
{
String dateString = // in "dow mon dd hh:mm:ss zzz yyyy" format
SimpleDateFormat dateFormat = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
Date convertedDate = dateFormat.parse(dateString);
System.out.println("Converted string to date : " + convertedDate);
}
public static void main(String[] argv)
{
new DateFormatTest();
}
}
}
Use simpledateformat. Find the doumentation here:
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Suppsoe you get String of "dateString" ;
SimpleDateFormat sdf = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
Date date = sdf.parse("dateString");
I'm parsing a date which is in format EEE, dd MMM yyyy HH:mm:ss Z. One of the sample values is Thu, 02 Sep 2010 04:03:10 -0700.
This is the parsing code:
SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
Date date = FORMATTER.parse(dateString);
This works absolutely fine if Phone Language is English but it throws parserException when language is changed to "France" or "Italian". I have even tried this:
SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.getDefault());
or for French locale more explicit:
SimpleDateFormat FORMATTER = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.French);
But no luck.....Can someone tell me what I am doing wrong?
Since "Thu" is English, you'll want to use Locale.ENGLISH to parse it.