【问题标题】:Java: Calculate month end date based on timezoneJava:根据时区计算月末日期
【发布时间】:2019-01-09 16:04:29
【问题描述】:

我有方法根据时区查找月末日期。

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CET"));
calendar.set(
    Calendar.DAY_OF_MONTH, 
    calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
);
System.out.println(calendar.getTime());`

它显示输出:Thu Aug 30 18:04:54 PDT 2018

但是,它应该给我 CET 的输出。

我错过了什么?

【问题讨论】:

  • 仅供参考,非常麻烦的旧日期时间类,例如 java.util.Datejava.util.Calendarjava.text.SimpleDateFormat 现在是 legacy,被 Java 8 中内置的 java.time 类所取代,之后。见Tutorial by Oracle
  • 不要依赖三四个字母的时区缩写,如 CET。大多数都是模棱两可的,而且很多,比如 CET,不是真正的时区。而是给出时区,例如欧洲/巴黎。使用 CET 的欧洲国家,在 8 月份使用 CEST,所以您的要求似乎相当——我应该说,矛盾,不可能吗?还是只是脱离了现实世界?
  • 为什么要在 CET 中输出? 8 月的最后一天是所有时区的 8 月 31 日。询问也是因为 java.time 的LocalDate 将为您提供独立于时区的信息。这不是更好吗?

标签: java calendar timezone


【解决方案1】:

Calendar.getTime() 方法返回一个 Date 对象,然后您将其打印到您的代码中。问题是Date不包含任何时区的概念,即使您已经使用Calendar.getInstance() 调用指定了时区。是的,这确实令人困惑。

因此,为了在特定时区打印Date 对象,您必须使用SimpleDateFormat 类,您必须在打印前调用SimpleDateFormat.setTimeZone() 来指定时区。

这是一个例子:

import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;

public class TimeZoneTest {

    public static void main(String argv[]){
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CET"));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        System.out.println("calendar.getTime(): " + calendar.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss z");
        sdf.setTimeZone(TimeZone.getTimeZone("CET"));
        System.out.println("sdf.format(): " + sdf.format(calendar.getTime()));
     }
}

这是我电脑上的输出:

calendar.getTime(): Fri Aug 31 01:40:17 UTC 2018
sdf.format(): 2018-Aug-31 03:40:17 CEST

【讨论】:

  • 谢谢你解释清楚。我对将时区设置为日历对象没有返回正确的时间感到困惑。
【解决方案2】:

这是因为 Date 对象没有时区作为其状态的一部分,而 getTime() 实际上返回一个与 JVM 时区相对应的日期,而您需要 SimpleDateFormat 来格式化和打印您的日期所需的时区。

如果您尝试添加以下代码行,您可以看到日历中的时区实际上是 CET。

System.out.println(calendar.getTimeZone().getDisplayName()); 

【讨论】:

    【解决方案3】:

    tl;博士

    YearMonth                         // Represent a year-month without day-of-month.
    .now(                             // Capture the current year-month as seen in the wall-clock time used by the people of a particular region (a time zone).
        ZoneId.of( "Africa/Tunis" )   // Specify your desired time zone. Never use 3-4 letter pseudo-zones such as `CET`. 
    )                                 // Returns a `YearMonth` object.
    .atEndOfMonth()                   // Determine the last day of this year-month. Returns a `LocalDate` object.
    .atStartOfDay(                    // Let java.time determine the first moment of the day. Not necessarily 00:00:00, could be 01:00:00 or some other time-of-day because of anomalies such as Daylight Saving Time (DST). 
        ZoneId.of( "Africa/Tunis" )  
    )                                 // Returns a `ZonedDateTime` object, representing a date, a time-of-day, and a time zone.
    

    java.time

    您正在使用多年前已被取代的糟糕的旧 Calendar 类,而是现代的 java.time 类。

    LocalDate

    如果您只需要日期,请使用LocalDate 类。那么时区与您的输出无关。

    但时区对于确定当前日期非常重要。对于任何给定的时刻,日期在全球各地都因地区而异。

    continent/region 的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 CETIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

    ZoneId z = ZoneId.of( "Europe/Paris" ) ;  // Or "Africa/Tunis" etc.
    LocalDate today = LocalDate.now( z ) ;    // Capture the current date as seen by the wall-clock time used by the people of a certain region (a time zone).
    

    YearMonth

    获取该日期的月份。用YearMonth 表示一年一个月。

    YearMonth ym = YearMonth.from( today ) ;
    

    或者跳过LocalDate

    YearMonth ym = YearMonth.now( z ) ;
    

    获取月底。

    LocalDate endOfThisMonth = ym.atEndOfMonth() ;
    

    ISO 8601

    要生成代表LocalDate 对象值的String,请调用toString。默认格式取自ISO 8601 标准。对于将是 YYYY-MM-DD 的仅日期值,例如 2018-01-23

    String output = endOfThisMonth.toString() ;
    

    如果您需要其他格式,请使用DateTimeFormatter 类。在 Stack Overflow 上搜索许多示例和讨论。

    时刻

    如果您需要一点时间,可以将时间和时区添加到您的 LocalDate 以获取 ZonedDateTime。或者让ZonedDateTime 确定一天中的第一个时刻(始终是 00:00:00!)。

    ZonedDateTime zdt = LocalDate.atStartOfDay( z ) ;
    

    关于java.time

    java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

    Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

    要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

    您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。

    从哪里获得 java.time 类?

    ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多