SimpleDateFormat gives ParseException - java

I'm fairly new here. I'm used to working with C# but I'm a newbie in java.
I'm trying to get an hour String out of a jSpinner (Date model), but I keep getting errors. I've looked into some answers that are already given here... but it still doesn't work.
uurStr returns "Sat Jan 25 16:09:49 CET 2014", I'm trying to get "16:09" out of it. But no luck so far.... any ideas?
The exception is thrown on the "uurDate = sdf.parse(uurStr);" part.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
SimpleDateFormat outputFmt = new SimpleDateFormat("HH:mm");
String uurStr = String.valueOf(jSpinner1.getValue());
Date uurDate = null;
try {
uurDate = sdf.parse(uurStr);
} catch (ParseException ex) {
Logger.getLogger(frmBackEnd.class.getName()).log(Level.SEVERE, null, ex);
}
String uur = outputFmt.format(uurDate);
JOptionPane.showMessageDialog(null, uur);

In order to correctly parse the weekday and timezone information, which both are language specific, it is necessary to set the locale as follows:
final SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
If the locale is not set, then the default locale of the JRE is taken. This may be the OS default locale or a user specific locale or the locale may have been overridden by another Java class with Locale.setDefault(Locale.XXXX). In any of these cases, this may be the correct locale or not.
That said, it is usually safer to set the timezone or you may get wrong time information if the timezone is not available in the string to be parsed:
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
In your example the timezone is available and you don't have to worry about that.

Related

Is the following DateFormat able to generate locale indepdent date string

My ideal way, of having a locale independent date string, is by using the following code
DateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy", Locale.ENGLISH);
which means, if I run the above code in a Arabic device, or a United States device, both will generate same string.
For the above case, dateFormat.format(date) will produce Dec 26, 2015.
I came across a legacy code, which is using the following way to generate locale independent date string.
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.ENGLISH);
Will the above code be truly locale independent. Will it "mistakenly" generate string like 26 Dec, 2015?
I wrote a simple code, to test both on Windows and Android device.
for (Locale locale : Locale.getAvailableLocales()) {
Locale.setDefault(locale);
DateFormat dateFormat0 = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.ENGLISH);
DateFormat dateFormat1 = new SimpleDateFormat("MMM d, yyyy", Locale.ENGLISH);
if (false == dateFormat0.format(date).equals(dateFormat1.format(date))) {
throw new java.lang.RuntimeException("Opps");
}
}
No exception was being thrown.
I was wondering, can we assume that DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.ENGLISH) will generate same date string too across different devices? Is there any edge case I didn't handle?
Using "MMM dd, yyyy"you retain control of the output, where by using the integer constant DateFormat.DEFAULT you are leaving it up to the locale definitions for the platform. They should be identical across these platforms for any given locale, but there really are no guarantee.
The DateFormat.DEFAULT parameter is only useful if you work with different Locales, as it'll allow DateFormat to create the proper localized date string.
Example:
DateFormat dfEn = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.ENGLISH);
DateFormat dfFr = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.FRENCH);
DateFormat dfGe = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.GERMAN);
System.out.println("ENGLISH: " + dfEn.format(date));
System.out.println("FRENCH : " + dfFr.format(date));
System.out.println("GERMAN : " + dfGe.format(date));
prints:
ENGLISH: Dec 26, 2015
FRENCH : 26 déc. 2015
GERMAN : 26.12.2015
I you want to be sure that the rendered date looks always the same it's the best choice to define the format explicitely as you did in your first code snippet:
DateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH);
The given locale here does not define the formatting but the wording to be used, here the name of the month is in english ("Dec"), rather than e.g. in german ("Dez").

SimpleDateFormat and parseException

I am developing a Web application into GWT and I am using the Object DatePicker. This object retrieves the date in a defined format which I am translating into a String such as:
Wed May 14 2014 00:00
For me it is useful to use this date as String for some operations. However, for one of them I need the Timestamp object. For that reason, I am making use of the SimpleDateFormat object in the following way:
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm");
Timestamp tDateIni = new Timestamp(sdf.parse(sDateIni).getTime());
Yet, when I run the remote debug I get a ParseException. Do you know what could the mistake be? I think I am using in a bad format the SimpleDateFormat object in the part "E MMM", but I am not sure. Thanks a lot in advance!
If you want to parse the date at client side in GWT then try with DateTimeFormat
DateTimeFormat dateTimeFormat=DateTimeFormat.getFormat("E MMM dd yyyy HH:mm");
Date date=dateTimeFormat.parse("Wed May 14 2014 00:00");
If you want to parse the date at server side then pass the time in milliseconds as long value instead of date string from client side and form the date back at server side using new Date(timeInMills)
Your date format uses the day of the week format that requires "EEE" instead of "E". This is causing the exception when the program is trying to read in your date string. It is expecting one letter for the day of the week.
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Change this from
SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd yyyy HH:mm");
to
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm");
It should be EEE instead of E to represent Weekdays like Wed
Below code, perfectly works (TESTED)
public static void main(String[] args) {
String s = "Wed May 14 2014 00:00";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm");
try {
Timestamp tDateIni = new Timestamp(sdf.parse(s).getTime());
System.out.println(tDateIni.getTime());
} catch (ParseException ex) {
System.out.println("Parse Error");
}
}
I have added the Locale object in the SimpleDateFormat object and now it works. Thank you for all your help and your comments!!!

Convert String to Date error parsing

I have looked at many examples and can still not find an answer to match my problem. I have now been stuck for an hour and need help.
I have many strings that are formated like this:
Wed, 06 Nov 2013 18:14:02
I have created a function to convert these strings into Date:
private static Date toDate(String pubDateString) {
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
pubDateString = pubDateString.substring(0, 25);
Date date = null;
try {
date = dateFormat.parse(pubDateString);
} catch (ParseException e1) {
e1.printStackTrace();
}
return date;
}
What I get is a ParseException:
java.text.ParseException: Unparseable date: "Wed, 06 Nov 2013 18:14:02"
Could anyone help me on my first challenge? Thanks.
edit : I tried HH, and kk
(When I originally answered the question, the format string used hh - it has since been changed.)
You're using hh for the hours part, which is a 12-hour format - but providing a string which uses "18". You want HH.
Additionally, I'd suggest explicitly specifying the locale if you know that the values will always use English names.
I've verified that if you specify the locale explicitly, the code definitely works - at least under Oracle's Java 7 implementation:
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss",
Locale.US);
If it wasn't working for you without the locale being specified (but with HH) that's probably why - presumably your system locale isn't English, so it was expecting different month and day names.

the exception with parsing a date YYYY-MM-DD HH:mm:ss

I try to parse a date in the format YYYY-MM-DD HH:mm:ss
String now = "2012-11-02 12:02:00";
DateFormat formatter;
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date_temp = (Date) formatter.parse(now.toString());
System.out.println("output: " + date_temp);
It gives me following exception
java.text.ParseException: Unparseable date: "2012-11-02 12:02:00"
Well yes, you've created a formatted with one format ("EEE MMM dd HH:mm:ss z yyyy") and then given it a string in a completely different format to parse. Why did you think that would work? Try this:
// Locale specified to avoid any cultural differences. You may also
// want to specify the time zone.
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
Locale.US);
Date date = formatter.parse(now);
Note that the parsed Date does not know anything about formatting - the result of calling toString() (as you're doing implicitly here) is always just the default format, in the JRE default time zone. If you want to print it out with a particular format, use SimpleDateFormat again.
Also note that I've combined declaration and initialization for the variable. Prefer that over declaring a variable in one line and giving it an initial value later.
Of course your date string is not in the format you are using in SimpleDateFormat. So it won't be able to parse it into a date object.
Try using this: -
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Parse a date with the timezone "Etc/GMT"

My first attempt was:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date date = formatter.parse(string);
It throws ParseException, so I found this hack:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
TimeZone timeZone = TimeZone.getTimeZone("Etc/GMT");
formatter.setTimeZone(timeZone);
Date date = formatter.parse(string);
It did not work either, and now I'm stuck. It parses without problems if I just change the timezone to "GMT".
edit: An example string to parse would be "2011-11-29 10:40:24 Etc/GMT"
edit2: I would prefer not to remove timezone information completely. I am coding a server that receives the date from an external user, so perhaps other dates will have other timezones.
To be more precise: This specific date I receive is from the receipt from the apple server after making an in app purchase on an iphone app, but I could also receive dates from other sources.
Don't know if this question is still relevant to you, but if you use Joda time, this'll work:
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss ZZZ").parseDateTime(s)
Without Joda time the following will work (bit more work though):
String s = "2011-11-29 10:40:24 Etc/GMT";
// split the input in a date and a timezone part
int lastSpaceIndex = s.lastIndexOf(' ');
String dateString = s.substring(0, lastSpaceIndex);
String timeZoneString = s.substring(lastSpaceIndex + 1);
// convert the timezone to an actual TimeZone object
// and feed that to the formatter
TimeZone zone = TimeZone.getTimeZone(timeZoneString);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(zone);
// parse the timezoneless part
Date date = formatter.parse(dateString);
It didn't work for me either the thing is I tried setting TimeZone of SimpleDateFormatter to "Etc/GMT" and then formatted a new date here is the output:
2011-11-30 10:46:32 GMT+00:00
So Etc/GMT is being translated as GMT+00:00
If you really want to stick to parse "2011-09-02 10:26:35 Etc/GMT" then following will help too without even considering explicit Timezone change:
java.text.SimpleDateFormat isoFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss 'Etc/GMT'");
isoFormat.parse("2010-05-23 09:01:02 Etc/GMT");
Works fine.
Following code is working for me
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Etc/GMT"));
try { System.out.println( sdf.parse("2011-09-02 10:26:35 Etc/GMT") );
} catch (ParseException e){
e.printStackTrace();
}

Categories