作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
记录是根据美国时区保存的,但是如果我想向用户显示相同的记录,它应该将服务器日期时间(美国时区)转换为用户的日期时间和用户时区
最佳答案
如果您在 google 中输入“Java date change timezone”或“Javascript date change timezone”。您将得到以下结果之一:
在 Java 中(来源:http://www.coderanch.com/t/417443/java/java/Convert-Date-one-timezone-another)
Date date = new Date();
DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");
formatter.setTimeZone(TimeZone.getTimeZone("CET"));
// Prints the date in the CET timezone
System.out.println(formatter.format(date));
// Set the formatter to use a different timezone
formatter.setTimeZone(TimeZone.getTimeZone("IST"));
// Prints the date in the IST timezone
System.out.println(formatter.format(date));
Javascript(来源:http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329)
// function to calculate local time
// in a different city
// given the city's UTC offset
function calcTime(city, offset) {
// create Date object for current location
d = new Date();
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
nd = new Date(utc + (3600000*offset));
// return time as a string
return "The local time in " + city + " is " + nd.toLocaleString();
}
// get Bombay time
alert(calcTime('Bombay', '+5.5'));
关于java - 如何将日期时间从一个时区转换为另一个时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238661/
我是一名优秀的程序员,十分优秀!