Skip navigation links
Java™ Platform
Standard Ed. 8

Package java.time.chrono

除默认ISO之外的日历系统的通用API。

See: 描述

Package java.time.chrono Description

除默认ISO之外的日历系统的通用API。

主要API基于ISO-8601中定义的日历系统。 但是,还有其他日历系统,这个包提供了基本的支持。 备用日历在java.time.chrono包中提供。

日历系统由Chronology界面定义,而日历系统中的日期由ChronoLocalDate界面定义。

意图是应用程序尽可能使用主API,包括从持久数据存储(如数据库)读取和写入代码,以及通过网络发送日期和时间。 然后在用户界面级别使用“计时”类来处理本地化的输入/输出。 有关问题的全面讨论,请参阅ChronoLocalDate

在应用程序中使用非ISO日历系统会引起显着的额外复杂性。 确保中的警告和建议ChronoLocalDate已经与“计时”接口之前,请先阅读。

支持的日历系统包括:

此示例列出了所有可用日历的今天日期。

  // Enumerate the list of available calendars and print todays date for each.
       Set<Chronology> chronos = Chronology.getAvailableChronologies();
       for (Chronology chrono : chronos) {
           ChronoLocalDate date = chrono.dateNow();
           System.out.printf("   %20s: %s%n", chrono.getId(), date.toString());
       } 

此示例在命名的非ISO日历系统中创建和使用日期。

  // Print the Thai Buddhist date
       ChronoLocalDate now1 = Chronology.of("ThaiBuddhist").dateNow();
       int day = now1.get(ChronoField.DAY_OF_MONTH);
       int dow = now1.get(ChronoField.DAY_OF_WEEK);
       int month = now1.get(ChronoField.MONTH_OF_YEAR);
       int year = now1.get(ChronoField.YEAR);
       System.out.printf("  Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),
                 dow, day, month, year);
   // Print today's date and the last day of the year for the Thai Buddhist Calendar.
       ChronoLocalDate first = now1
                 .with(ChronoField.DAY_OF_MONTH, 1)
                 .with(ChronoField.MONTH_OF_YEAR, 1);
       ChronoLocalDate last = first
                 .plus(1, ChronoUnit.YEARS)
                 .minus(1, ChronoUnit.DAYS);
       System.out.printf("  %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),
                 first, last); 

此示例创建并使用特定ThaiBuddhist日历系统中的日期。

  // Print the Thai Buddhist date
       ThaiBuddhistDate now1 = ThaiBuddhistDate.now();
       int day = now1.get(ChronoField.DAY_OF_MONTH);
       int dow = now1.get(ChronoField.DAY_OF_WEEK);
       int month = now1.get(ChronoField.MONTH_OF_YEAR);
       int year = now1.get(ChronoField.YEAR);
       System.out.printf("  Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),
                 dow, day, month, year);

   // Print today's date and the last day of the year for the Thai Buddhist Calendar.
       ThaiBuddhistDate first = now1
                 .with(ChronoField.DAY_OF_MONTH, 1)
                 .with(ChronoField.MONTH_OF_YEAR, 1);
       ThaiBuddhistDate last = first
                 .plus(1, ChronoUnit.YEARS)
                 .minus(1, ChronoUnit.DAYS);
       System.out.printf("  %s: 1st of year: %s; end of year: %s%n", last.getChronology().getId(),
                 first, last); 

包装规格

除非另有说明,否则在此包中的任何类或接口中将null参数传递给构造函数或方法将导致抛出NullPointerException Javadoc“@param”定义用于总结空行为。 “@throws NullPointerException ”没有在每个方法中明确记录。

所有计算应检查数字溢出并抛出ArithmeticExceptionDateTimeException

从以下版本开始:
JDK1.8
Skip navigation links
Java™ Platform
Standard Ed. 8

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.

本帮助文档是使用 《谷歌翻译》翻译,请与英文版配合使用