If you do not already know the user's time zone, you will need client-side JavaScript to send this information to your server.
var d =new Date(); d.getTimezoneOffset();
Use the time zone offset to select the time zone, and use this time zone when formatting the date, as other answers explain.
Actually, on the other hand, there may be an easier way to satisfy your requirements. If you want to format the date on the server side, as I mentioned above, let me know. Otherwise, this method could be much simpler ...
If your formatted string should always read YYYY-MM-DD ..., then include the following code in the page section:
<script type="text/javascript" language='JavaScript'> <!-- function formatDate(millis) { var d =new Date(millis); return d.getFullYear() +"-" +(d.getMonth() +1) +"-" +d.getDate() +" " +d.getHours() +":" +d.getMinutes() +":" +d.getSeconds() +"." +d.getMilliseconds(); } </script>
Then, wherever you want the formatted date on the page, enable the following JavaScript
<script type="text/javascript" language='JavaScript'> </script>
Replacing XXXX with a date, in milliseconds. For example, if you create a page using JSP, then in your Java code use
request.setAttribute("dateInMillis", date.getTime());
and then in your JSP use javascript from above, replacing XXXX as follows:
document.write(formatDate(${dateInMillis}));