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.
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'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);
This question already has answers here:
Java date format convert on Android
(8 answers)
Closed 8 years ago.
I need to create a SimpleDateFormat in order to parse this date and convert it to another format but I dont know how to pass it into the constructor:
SimpleDateFormat sdf = new SimpleDateFormat("???");
Your date format should be
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.ENGLISH);
See here for more info.
Here is a DateFormat cheat sheet:
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Where
E = Day in week
d = Day in month
M = Month in year
y = Year
H = Hour in day
m = Minute in hour
s = Second in minute
z = Time zone
For more info see SimpleDateFormat
"Sat, 26 Jul 2014 11:55:55 GMT"
"EEE, d MMM yyyy HH:mm:ss z"
sdf1 = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.ENGLISH);
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.
What's wrong with below date format? I'm getting java.text.ParseException: Unparseable date Thu, 03 May 2012 14:00:00 CEST
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
Date parsedDate = DATE_FORMATTER.parse(inputDate);
I have tried below combinations but am out of luck:
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz", Locale.US);
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
I'm getting the same exception if modify CEST to CET, but not for PST. Any pointer will be appreciated. Thanks.
It works for me too
package test.java;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
Date parsedDate = DATE_FORMATTER.parse(inputDate);
System.out.println("Date = " + parsedDate);
}
}
And the output I get is:
Date = Thu May 03 13:00:00 BST 2012
Its working... see this..
- Please correct the typo from inputDateString to inputDate in parse()
Its a Working Java Code... Modify it to suit your Android use....
public class CopyArray {
public static void main(String[] args) {
String inputDate = "Thu, 03 May 2012 14:00:00 CEST";
SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z");
try {
Date parsedDate = DATE_FORMATTER.parse(inputDate);
System.out.println(parsedDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Aside from the minor issue that you are trying to parse inputDateString when your variable is actually called inputDate, I'm able to run your code with no exceptions.