Thursday, December 14, 2006

Locale Dependent DateFormat

Recently I came across a strange problem on the debian server install.

The following piece of jython code which was working fine on my machine failed to work on debian server.

import java.text.DateFormat as DateFormat
format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
format.setLenient(1);
print format.parse('Nov 6, 2006 3:23:54 PM');

I was getting this exception:
Traceback (innermost last):
File "<string>", line 5, in ?
java.text.ParseException: Unparseable date: "Nov 6, 2006 3:23:54 PM"
at java.text.DateFormat.parse(DateFormat.java:335)
...

I looked at the default Locale that was used and it was: [command used: locale]
LANG=en_IN
LC_CTYPE="en_IN"
LC_NUMERIC="en_IN"
LC_TIME="en_IN"
LC_COLLATE="en_IN"
LC_MONETARY="en_IN"
LC_MESSAGES="en_IN"
LC_PAPER="en_IN"
LC_NAME="en_IN"
LC_ADDRESS="en_IN"
LC_TELEPHONE="en_IN"
LC_MEASUREMENT="en_IN"
LC_IDENTIFICATION="en_IN"
LC_ALL=

I did the following and got the piece of code working:
export LC_CTYPE="en_US.UTF-8"
[If this does not solve try export LC_TIME="en_US.UTF-8"]

After this I could got the following output:
Mon Nov 06 15:23:54 IST 2006

Make sure you set the right LOCALE before using any API that depends on SYSTEM LOCALE information. Few Java API's allows you to specify the Locale as an argument use it when required.

More details about Java 1.3 Locale

I found a related post Don't forget to specify the locale

Comments:
Thanks for the useful information
 
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?