Use local user setting to display date in Java

I have a grails project

Customer customer = Customer.get(params.id) 

where the client is a domain class

I need to display

 Date date = customer.createDate 

in local user setting

I can use java or grails or groovy or html or javascript to do this

Example:

 if date = "2012-01-01 00:00:00.0" in GMT then user must display it as "2012-01-01 01:00:00.0" in London (GMT +1) 

any help for using local user setting to display date?

+4
source share
7 answers

show this site http://www.pageloom.com/automatic-timezone-detection-with-javascript useful

download jstz.min.js and add the function to the html page

 <script language="javascript"> function getTimezoneName() { timezone = jstz.determine_timezone() return timezone.name(); } </script> 

and call this function with a tag

+2
source

Java has a java.text.DateFormat class for formatting dates. You will need to pass Locale to the user to get an instance to display the date in the user's preferred language. And you will need to set user TimeZone to DateFormat to display the date in the user time zone.

0
source

Since you are using the Grails framework, I would suggest you look at g: formatDate . It allows you to specify the format as well as the locale (inside it uses SimpleDateFormat).

0
source

some dummy codes as below:

 SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG, Locale.CHINA).format(customer.createDate()).toUpperCase(); 
0
source

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(); // send this value to the server 

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'> <!-- document.write(formatDate(XXXX)); // --> </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})); 
0
source

Grails has a <g:formatDate /> . In the controller, you can name it as the g.formatDate () method. I think it uses the current locale of the user for the rendering date.

0
source

I know this is an old question, but I recently created a Grails plugin that uses the jstz javascript library to solve this problem for you:

https://grails.org/plugin/timezone-detection

0
source

Source: https://habr.com/ru/post/1389621/


All Articles