Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/Servlet: get current sql.Date

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?

like image 778
Artemij Avatar asked May 06 '09 12:05

Artemij


1 Answers

Even a more compact solution ;)

java.sql.Date timeNow = new Date(Calendar.getInstance().getTimeInMillis());
like image 70
Hugh Avatar answered Oct 09 '22 01:10

Hugh