java - 在java中根据客户端/服务器偏移显示本地时间

标签 java calendar timezone client gmt

我的客户端/浏览器位于印度,我从​​ javascript 获取时区偏移 使用以下代码:

var now = new Date();
var localOffSet = now.getTimezoneOffset(); -330 // for India
int localOffSetMin = (localOffSet)*(-1); 

我的服务器位于纽约,因此我使用以下方法获取它的偏移量:

 TimeZone timeZone = now.getTimeZone();
 int serverOffset = timeZone.getRawOffset();
 int serverOffSetMinutes = serverOffset / 60000; // -300 for America/New York

为了找到我的机器上的本地时间,我使用这个:

int offSets = Math.abs(serverOffSetMinutes-localOffSetMin); 

now.setTime(createDt); // createDt is date field value for some column
now.add(Calendar.MINUTE, offSets); // adds offset  
Date localDt = now.getTime(); 

但是我得到的日期/时间比预期时间早 1 小时。我错过了什么?

最佳答案

使用 Java SE 进行日期和时间操作

You can print a list of supported TimeZones by using the following code.

System.out.println(TimeZone.getAvailableIDs().toString());

然后您可以使用以下代码查找并打印时区之间的差异。您必须注意夏令时。

public void printTimeZoneDifference(String from, String to) {
   TimeZone easternStandardTime = TimeZone.getTimeZone(from);
   TimeZone indiaStandardTime = TimeZone.getTimeZone(to);

   long milliseconds = easternStandardTime.getRawOffset() - indiaStandardTime.getRawOffset() + easternStandardTime.getDSTSavings() - indiaStandardTime.getDSTSavings();
   String difference = String.format("%02d min, %02d sec", TimeUnit.MILLISECONDS.toMinutes(milliseconds), TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)));

   System.out.println("The difference in time between" + easternStandardTime.getDisplayName() + " and " + indiaStandardTime.getDisplayName() + " is " + difference);
 }

尽管如果我要写这样的东西,我可能会传递一个 TimeZone 对象作为参数,并让该方法单独负责减法。然后我要么打印结果,要么将其作为不同方法的一部分。我没有以这种方式构建帖子,因为我想在帖子中包含所有相关代码。

使用 Joda 处理日期和时间

This type of manipulation has already been solved in Java. The Joda Time Library is probably your best bet if you are doing a lot of date manipulation. If you are only manipulating time in this one instance then it would be a bit over kill to include the dependency in your runtime.

再次打印出时区。

public void printDateTimeZones() {
   for(String zone : DateTimeZone.getAvailableIDs()) {
      System.out.println(zone);
   }
}

然后,您可以使用以下代码使用默认格式返回两个 DateTimeZone 之间的周期(差异)的字符串。

public String printPeriod(String from, String to) {
   Period period = new Period(new DateTime(DateTimeZone.forID(to)), new DateTime(DateTimeZone.forID(from)));
   return PeriodFormat.getDefault().print(period);
}

类似地,Joda 提供了一个格式构建器类,允许您指定您喜欢的格式。

public String printPeriod(String from, String to) {
   PeriodFormatter formatter = new PeriodFormatterBuilder()
     .printZeroRarelyFirst()
     .appendYears().appendSuffix(" Years").appendSeparator(",")
     .appendMonths().appendSuffix(" Months").appendSeparator(",")
     .appendWeeks().appendSuffix(" Weeks").appendSeparator(",")
     .appendDays().appendSuffix(" Days").appendSeparator(",")
     .appendHours().appendSuffix(" Hours").appendSeparator(",")
     .appendSeconds().appendSuffix(" Seconds").appendSeparator(",")
     .appendMillis().appendSuffix(" Milliseconds")
   .toFormatter();

   return formatter.print(new Period(new DateTime(DateTimeZone.forID(from)), new DateTime(DateTimeZone.forID(to))));
}

关于java - 在java中根据客户端/服务器偏移显示本地时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44889018/

相关文章:

java - 表示日历对象的最佳规范字符串?

ios - 将 .ics 导入 iOS 应用

python - 时区和本地化

Java - 为什么将具体实现作为参数发送到通用接口(interface)无法编译?

java - 在 Spring Security 中从文件中读取/添加用户凭据

primefaces - 如何在 Primefaces 的日历组件中设置年份列表?

用于转换为任何本地时间的 C++ 库(由 linux zoneinfo 字符串定义)

java - 如何在 android 中将日期从 GMT TimeZone 解析为 IST TimeZone 反之亦然

java - 在 Java 中,从内部(类)对象访问 'outer object'?

java - 难以理解代码,特别是 &