Meaningless Drivel is fun!
The moose likes Servlets and the fly likes how to get client's date and time in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to get client Watch "how to get client New topic

how to get client's date and time in servlet

Akash Abrol

Joined: Jan 30, 2006
Posts: 9

i am working on an website that will be accessed by users along different timezones. As a requirement of the website, i need to get the client's date and time at the server side. Similarly, i need to convert server's date into client's date before sending any date related information to the client.

I have seen getLocale() method of HttpServletRequest but it doesn't provide any information about client's date and time....

Can anybody help....

[ March 22, 2006: Message edited by: Akash Abrol ]
karthikeyan piramanayagam

Joined: Feb 16, 2006
Posts: 22
See to Trace the information about a client you can use the following


try with this... but i'm not exactly know about the date and time. you use the above thing and search in google, u may get some ideas. All the best

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
You can use java script and a hidden variable to store and retrurn the date. But in case the customisation is required on the first page that is accessed them it might be difficult with using servlets only.
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
you can try using the request.getDateHeader("Date") ;
this is, however, normally only used by browsers on a
put or post request, and is, even then, optional, so
for the most part, it probably will not be there.
Otherwise, javascript and a hidden field in forms would
be the way to go. If it is not a form, but a link, then
javascript for a little url rewriting (?date=date) would
also work. Both are defeated by having javascript
turned off though. In general, clients "should" attempt
to provide as little data about themselves as possible,
which makes it hard to get this kind of data.
[ March 22, 2006: Message edited by: Martin Simons ]
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
There are a few other ways.

You could include a small applet on the page which reads the time from the browser and sends it direct to the server.

What most time-sensitive applications do, though is to use a cookie containing a time offset from server time. Typically the web application might try to read the cookie on each communication from a client browser - if the cookie is absent, redirect to a "whast time is it?" page, then set the cookie based on the answer before forwarding to the real destination page. Much like login, really.

A combination of the two approaches above would look for the cookie, if the cookie is not found, send a page containing a small applet which sends back the client time, then the web app sets the cookie based on the message from the applet. Almost magic, and no URL-rewriting or JavaScript needed!

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Akash Abrol

Joined: Jan 30, 2006
Posts: 9
Is there any other way of getting client's date and time EXCEPT JavaScript or Cookies. The date and time information is very critical for my application. So i dont want to rely on Javascript or Cookies.

Manesh Kumar
Ranch Hand

Joined: Mar 21, 2006
Posts: 94
I dont think it is possible to get reliable answer without using Javascript.
There is nothing to worry about Javascript, since majority of applications on the web are not possible without using Javascript.
With advent of new web technologies, Javascript has to be supported by browser.

So I think you can safely rely on it.

Have a initial home page which will be accessed and sent to the browser.
On that page write java script code on onload function.

window.location.href = "http://yourlocation/myactualhome?date="+ new Date();

This will redirect to you actual home page and you can get the date and time as parameter.

May be this helps.

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Though you can but it not a very nice of doing it.

Get the IP for the request. Find to which geographic region it maps to and change the time accordingly.

But I guess that applets or the Javascript method is better.
I agree. Here's the link:
subject: how to get client's date and time in servlet
It's not a secret anymore!