Parse Date, CET vs CEST, invalid TimeZone, unparseable - java

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)

Related

ParseException when parse date via SimpleDateFormat

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

java.text.ParseException: Unparseable date Exception while converting timestamp value to time in java

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.

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

Java:Date format conversion issue

I have a date of the format EEE MMM dd HH:mm:ss zzz yyyy
I have to convert this to dd/MM/yyyy
I did the following:
SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat fmtddMMyyyy = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date d = fmt.parse("Mon May 28 00:00:00 IST 2012");<br>
String formattedDate = fmtddMMyyyy.format(d);
When I tried to print d it displays 0027833001988071567.
Where am I going wrong?
I have to guess that your real code is only marginally connected to what you show. Due to some experiments and the documentation.
Possibly the original parsing throws an exception and what every you print has nothing to do with you date manipulation.
In order to fix the problem
print the string you want to convert
print the parsed date
print the converted String
Use the constructor variants with Locale argument so everybody on stackoverflow can reproduce it, and you application works the same wherever it is running.
All this in a simple class containing nothing but a main method which contains the code.
If the problem persists, come back with the code and its output.
With my advice partially applied you might end with this:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class Experiment {
public static void main(String args[]) throws ParseException {
SimpleDateFormat fmt = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
SimpleDateFormat fmtddMMyyyy = new SimpleDateFormat("dd/MM/yyyy",
Locale.US);
java.util.Date d = fmt.parse("Mon May 28 00:00:00 IST 2012");
String formattedDate = fmtddMMyyyy.format(d);
System.out.println(formattedDate);
}
}
Which prints out
28/05/2012
try:
String formattedDate = fmtddMMyyyy.format(d);
UPD: Well I got the following solution:
SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
works fine (without locale defining wasn't working for me too). Because you define the month name and day-of-week name according to English language, not your local, as I presume.

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