简体   繁体   中英

Getting user time zone and converting utc to user time zone

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;

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM