views:

1344

answers:

3

What I need to do is:

1) Get user's locale from request.

2) Create new sql.Date object with current date and time, based on user's locale

3) Write it in MySQL db, column type: TIMESTAMP

What I got is:

java.util.Locale locale = request.getLocale();

java.text.DateFormat dateFormat =
java.text.DateFormat.getDateTimeInstance(
                        java.text.DateFormat.LONG, java.text.DateFormat.LONG, locale );

java.util.Date date = new java.util.Date();

java.sql.Date sqlDate = new java.sql.Date( date.getTime() );

Date is OK, but have a problem with time - always 12:00:00 AM.

Any suggestions? Is there a more efficient way to do this?

+2  A: 

If you want the time, you need a java.sql.Timestamp not a java.sql.Date, and a timestamp column in the database.

Paul Tomblin
Exactly. Thanks for a hint!
Artemij
A: 

Note that request.getLocale uses the value of Accept-Language heading and may not always give you the correct locale.

kgiannakakis
I'll keep it in mind. Thank you.
Artemij
A: 

Getting the time zone based on the locale may not be their actual time zone .

An example: Here in Australia, the locale is 'en-AU', but we have a few time zones with up to 3 hours difference.

I'd guess in the US they have this problem too.

A possible solution is to store UTC time instead. If the user then adjusts his timezone in, say, his prefrences, or you use some other method of passing the time zone (client/browser info say via AJAX) then you can adjust the UTC time based on that using a java.util.Calendar instance.

Clinton
Thank you for that important note. Can you suggest an example of elegant solution?
Artemij