简体   繁体   English

获取用户时区并将utc转换为用户时区

[英]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. 伙计们,我在spring控制器中使用一些代码来获取用户时区(“代码在下面给出”),因为我们开发的Web应用程序的服务器位于阿联酋,而datetime中给出的时间正以UTC的形式到达服务器时间。 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 因此,我使用这种方法来获取用户时区,并且尝试将UTC时间转换为用户时区,但是很遗憾,这种转换无法正常工作,请帮助我

// 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, UTC转换为IST,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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