Record Class CardDate

java.lang.Object
java.lang.Record
dev.hloth.zgztransport.CardDate
Record Components:
year - 2000 to 2127
month - 1 to 12
day - 1 to 31
All Implemented Interfaces:
Encodable, Comparable<CardDate>

public record CardDate(int year, int month, int day) extends Record implements Comparable<CardDate>, Encodable
A date as the card packs it into two bytes: 7 bits of year since 2000, 4 bits of month and 5 bits of day.

The card does not check the calendar, so a day of 31 is accepted in any month; this type accepts the same values instead of rejecting dumps that hold them. Use toLocalDate() to reach a calendar date.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Bytes a packed date takes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    CardDate(int year, int month, int day)
    Checks the ranges of the three fields.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    int
    day()
    Returns the value of the day record component.
    static CardDate
    decode(byte[] bytes)
    Decodes the two big-endian bytes of a packed date.
    byte[]
    Encodes this date into two big-endian bytes.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    int
    Returns the value of the month record component.
    This date as a calendar date.
    The date in YYYY-MM-DD form.
    int
    Returns the value of the year record component.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • CardDate

      public CardDate(int year, int month, int day)
      Checks the ranges of the three fields.
      Throws:
      IllegalArgumentException - if a field is outside the range the bits allow
  • Method Details

    • decode

      public static CardDate decode(byte[] bytes)
      Decodes the two big-endian bytes of a packed date.
      Parameters:
      bytes - the two bytes
      Returns:
      the date they hold
      Throws:
      CardFormatException - if the input is not two bytes or holds a zero month or day
    • encode

      public byte[] encode()
      Encodes this date into two big-endian bytes.
      Specified by:
      encode in interface Encodable
      Returns:
      the two bytes
    • toLocalDate

      public Optional<LocalDate> toLocalDate()
      This date as a calendar date.
      Returns:
      the calendar date, or empty when the card holds a day the month does not have
    • compareTo

      public int compareTo(CardDate other)
      Specified by:
      compareTo in interface Comparable<CardDate>
    • toString

      public String toString()
      The date in YYYY-MM-DD form.
      Specified by:
      toString in class Record
      Returns:
      the printed date
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • year

      public int year()
      Returns the value of the year record component.
      Returns:
      the value of the year record component
    • month

      public int month()
      Returns the value of the month record component.
      Returns:
      the value of the month record component
    • day

      public int day()
      Returns the value of the day record component.
      Returns:
      the value of the day record component