0

Guy's, I am using some code in spring controller to get user time zone ('code is given below'), because the server of the web application we developed is located in UAE and the time given in datetime is getting to the server as UTC time. I am using this application from India, so that I want this time to be viewed as Indian Standard Time in the application. So I use this method to get the user time zone, and I try to convert UTC time to the user time zone, but unfortunately this conversion is not working, please help me

// code for getting time zone
TimeZone clientTimeZone =  Calendar.getInstance(httpServletRequest.getLocale()).getTimeZone();

// This is the code used to convert UTC to client time zone
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
sdf.setTimeZone(clientTimeZone);
String time = sdf.format(dateTime);
return time;
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
Shamith S Kumar
  • 97
  • 1
  • 1
  • 7
  • Your code seems fine. What problem are you facing? – Raman Sahasi Jan 18 '17 at 04:56
  • The UTC conversion to user time standerd does not work, while i deployed this code in server. This application can be used from anywhere in world, So this conversion is much needed – Shamith S Kumar Jan 18 '17 at 05:02
  • Have you debugged the server? What values of `clientTimeZone` and `dateTime` are you getting? – Raman Sahasi Jan 18 '17 at 05:03
  • I don't have the permission to access the server now. I will try it – Shamith S Kumar Jan 18 '17 at 05:22
  • Your method does not work. I tried filling 5 different locales into `Calendar.getInstance()` but all calendar instances came out with `Europe/Berlin` time zone, which is the default time zone for my computer. I don’t know a fix. It may be that you cannot deduce the user’s time zone from the servlet request (I hope there is a way). – Ole V.V. Jan 18 '17 at 07:24
  • See [how to get client's time zone in java from HttpServletRequest?](http://stackoverflow.com/questions/22479407/how-to-get-clients-time-zone-in-java-from-httpservletrequest). There are a couple of links in the answer. It seems folks generally use client-side JavaScript to obtain the client time zone. Your search engine may find other interesting stuff for you. – Ole V.V. Jan 18 '17 at 07:30
  • Possible duplicate of [how to get client's time zone in java from HttpServletRequest?](http://stackoverflow.com/questions/22479407/how-to-get-clients-time-zone-in-java-from-httpservletrequest) – Ole V.V. Jan 18 '17 at 07:31
  • I hope you will allow me to add: if you had made a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve), you should have identified the problem yourself before posting your question. – Ole V.V. Jan 18 '17 at 09:26
  • The only zone I am getting from HttpServletRequest is UTC but actual time Zone is IST. As far I searched for solution, I get to know that it is not possible.. – Shamith S Kumar Jan 24 '17 at 02:54

1 Answers1

-1

UTC to IST conversion,

Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone (TimeZone.getTimeZone ("IST"));
System.out.println ("Time in IST is " + sdf.format (now));
Karthi R
  • 1,338
  • 10
  • 25
  • 2
    Except for the difference between a 12 hours clock and a 24 hours clock, this is exactly what the asker is already trying in the question. I believe you are missing the root of the problem. – Ole V.V. Jan 18 '17 at 11:11