Why is adding weeks to java.time.Instant not supported?

前端 未结 5 1982
醉话见心
醉话见心 2021-01-18 05:50

The following piece of code:

Instant inFourWeeks = Instant.now().plus(4L, ChronoUnit.WEEKS);

Throws an exception:

java.time         


        
5条回答
  •  没有蜡笔的小新
    2021-01-18 06:16

    Other Answers are correct. I am adding this bit of clarification.

    Instant is basic building-block

    The Instant class is a basic building-block in the java.time classes. It represent a moment, a point on the timeline. Internally it is simply a count of whole seconds since the epoch reference of first moment of 1970 in UTC. So this class is deserving of little functionality.

    This building-block may be used to track a moment in any of many possible calendaring systems. Making sense of an Instant, to treat it as a date, week, month, and so on is up to the definitions of a particular calendaring system. A calendaring system can define any number of days in a week, or any number of months in a year, and so on, or may not even have such concepts as week or month.

    The most obvious calendar system is the modern ISO 8601 used throughout the West and other parts of the world. The OffsetDateTime & ZonedDateTime classes built on top of Instant to make up key parts of this ISO calendaring system. These classes are bundled with Instant merely because they are expected to be commonly used by many Java programmers. But they are by no means the only calendaring system.

    Look to the java.time.chrono package for these various calendar systems:

    • HijrahChronology
      The Hijrah calendar is a lunar calendar supporting Islamic calendars.
    • IsoChronology
    • JapaneseChronology
      The Japanese Imperial calendar system.
    • MinguoChronology
      The Minguo calendar system.
    • ThaiBuddhistChronology
      The Thai Buddhist calendar system.

    The ThreeTen-Extra project provides additional functionality to the java.time classes. This includes more calendaring systems:

    • AccountingChronology
      A proleptic 52/53-week Accounting calendar system per IRS Publication 538 and the International Financial Reporting Standards.
    • BritishCutoverChronology
      The British Julian-Gregorian cutover calendar system.
    • CopticChronology
      The Coptic calendar system.
    • DiscordianChronology
      The Discordian calendar system.
    • InternationalFixedChronology
      The International Fixed calendar system. Also known as the Eastman Kodak calendar.
    • JulianChronology
      The proleptic Julian calendar system, forerunner to the modern Gregorian and ISO calendars.
    • PaxChronology
      The Pax calendar system.
    • Symmetry010Chronology
    • Symmetry454Chronology
      The Symmetry454 calendar system.

    There may be yet more from third-parties that I am not aware of.

提交回复
热议问题