Search code examples
javajakarta-eevaadinvaadin6

How to get the time of Computer and not server Java vaadin


Hey how to get the time of the computer and not the server using vaadin because getDate return to me the time of the server but i want the client computer time ??


Solution

  • Vaadin has a class called WebBrowser that provides useful information about the client (browser):

    You can access the current WebBrowser instance for example as follows:

    public class MyApplication extends Application {
    
        @Override
        public void init() {
            setMainWindow(new Window());
    
            ApplicationContext context = this.getContext();
            if (context instanceof WebApplicationContext) {
                WebBrowser webBrowser = ((WebApplicationContext) this.getContext()).getBrowser();
    
                Date now = webBrowser.getCurrentDate(); // Returns the current date and time on the browser
            }
        }
    }
    

    For more information about the WebBrowser class, see its JavaDocs.