Sha*_*shI 9 java timezone datetime
记录将根据美国的时区保存,但如果我想向用户显示相同的记录,则应将服务器日期时间(美国时区)转换为用户的时区的用户日期时间
Nie*_*els 12
如果您输入谷歌"Java日期更改时区"或"Javascript日期更改时区".您将得到一个结果:
在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));
Run Code Online (Sandbox Code Playgroud)
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'));
Run Code Online (Sandbox Code Playgroud)
Bas*_*que 10
旧的日期时间类设计糟糕,令人困惑,麻烦.避免他们.
使用现代类:Java 8及更高版本中内置的java.time框架.查找早期Java 6和7以及Android的后端口.
Instant now = Instant.now();
Run Code Online (Sandbox Code Playgroud)
应用时区(ZoneId)来获得ZonedDateTime.
切勿使用3-4字母区域缩写,例如EST或IST.它们既不标准也不独特(!).使用适当的时区名称,continent/region以Asia/Kolkata,例如Pacific/Auckland,America/Los_Angeles.
ZoneId zoneId_Montreal = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt_Montreal = ZonedDateTime.ofInstant( instant , zoneId_Montreal );
Run Code Online (Sandbox Code Playgroud)
应用其他时区以生成另一个ZonedDateTime时区.打电话withZoneSameInstant.
ZoneId zoneId_Paris = ZoneId.of( "Europe/Paris" ); // Or "Asia/Kolkata", etc.
ZonedDateTime zdt_Paris = zdt_Montreal.withZoneSameInstant( zoneId_Paris );
Run Code Online (Sandbox Code Playgroud)
Instant instant = zdt_Paris.toInstant();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28036 次 |
| 最近记录: |