【问题标题】:Java util calendar - is it enough to set timezone to get time in another timezoneJava util calendar - 设置时区是否足以在另一个时区获取时间
【发布时间】:2015-03-17 10:08:17
【问题描述】:

我需要在另一个时区获取日历时间的小时值。我试过这个例子: https://stackoverflow.com/a/7695910/775523(java.util.Calendar 部分)但它为最后一个 cal.get(Calendar.HOUR_OF_DAY) 打印 15(我希望有 17 个!)。 那么在另一个时区获得小时数的正确方法是什么 - 我使用构建 1.7.0_21-b11。 谢谢。

【问题讨论】:

标签: java calendar timezone


【解决方案1】:

您可以使用此代码参考

    Date date = new Date();  
    DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");          
    formatter.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));  
    // 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("Australia/Melbourne"));  
    // Prints the date in the IST timezone  
    System.out.println(formatter.format(date));

你可以使用 date.getHours();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多