Java Code Examples for java.time.OffsetDateTime#getYear()

The following examples show how to use java.time.OffsetDateTime#getYear() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryGenerator.java    From pgadba with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * parses a OffsetDateTime to a byte array.
 *
 * @param input the OffsetDateTime to convert
 * @return a byte array
 */
public static byte[] fromOffsetDateTime(Object input) {
  if (input == null) {
    return new byte[]{};
  }

  if (input instanceof OffsetDateTime) {
    OffsetDateTime x = (OffsetDateTime) input;
    if (x == OffsetDateTime.MAX) {
      return "infinity".getBytes(StandardCharsets.UTF_8);
    } else if (x == OffsetDateTime.MIN) {
      return "-infinity".getBytes(StandardCharsets.UTF_8);
    }

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
      baos.write(x.format(offsetDateTimeFormatter).getBytes(StandardCharsets.UTF_8));

      if (x.getYear() < 0) {
        baos.write(" BC".getBytes(StandardCharsets.UTF_8));
      }

      return baos.toByteArray();
    } catch (IOException e) {
      throw new IllegalArgumentException("couldn't parse input to byte array", e);
    }
  }

  throw new RuntimeException(input.getClass().getName()
      + " can't be converted to byte[] to send as a OffsetDateTime to server");
}
 
Example 2
Source File: PNGMetadata.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
void initImageCreationTime(OffsetDateTime offsetDateTime) {
    // Check for incoming arguments
    if (offsetDateTime != null) {
        // set values that make up Standard/Document/ImageCreationTime
        creation_time_present = true;
        creation_time_year    = offsetDateTime.getYear();
        creation_time_month   = offsetDateTime.getMonthValue();
        creation_time_day     = offsetDateTime.getDayOfMonth();
        creation_time_hour    = offsetDateTime.getHour();
        creation_time_minute  = offsetDateTime.getMinute();
        creation_time_second  = offsetDateTime.getSecond();
        creation_time_offset  = offsetDateTime.getOffset();
    }
}
 
Example 3
Source File: DateTime.java    From cql_engine with Apache License 2.0 5 votes vote down vote up
public void setDateTime(OffsetDateTime dateTime) {
    if (dateTime.getYear() < 1) {
        throw new InvalidDateTime(String.format("The year: %d falls below the accepted bounds of 0001-9999.", dateTime.getYear()));
    }

    if (dateTime.getYear() > 9999) {
        throw new InvalidDateTime(String.format("The year: %d falls above the accepted bounds of 0001-9999.", dateTime.getYear()));
    }
    this.dateTime = dateTime;
}
 
Example 4
Source File: ASN1Type.java    From xipki with Apache License 2.0 5 votes vote down vote up
public static int writeGeneralizedTime(Date time, byte[] out, int offset) {
  OffsetDateTime offsetTime = time.toInstant().atOffset(ZoneOffset.UTC);
  int idx = offset;
  out[idx++] = 0x18;
  out[idx++] = 15;
  // yyyyMMddhhmmssZ
  // year
  int year = offsetTime.getYear();
  out[idx++] = (byte) (0x30 + year / 1000);
  out[idx++] = (byte) (0x30 + year / 100 % 10);
  out[idx++] = (byte) (0x30 + year / 10 % 10);
  out[idx++] = (byte) (0x30 + year % 10);
  // month
  int month = offsetTime.getMonthValue();
  out[idx++] = (byte) (0x30 + month / 10);
  out[idx++] = (byte) (0x30 + month % 10);
  // day
  int day = offsetTime.getDayOfMonth();
  out[idx++] = (byte) (0x30 + day / 10);
  out[idx++] = (byte) (0x30 + day % 10);
  // hour
  int hour = offsetTime.getHour();
  out[idx++] = (byte) (0x30 + hour / 10);
  out[idx++] = (byte) (0x30 + hour % 10);
  // minute
  int minute = offsetTime.getMinute();
  out[idx++] = (byte) (0x30 + minute / 10);
  out[idx++] = (byte) (0x30 + minute % 10);
  // second
  int second = offsetTime.getSecond();
  out[idx++] = (byte) (0x30 + second / 10);
  out[idx++] = (byte) (0x30 + second % 10);
  out[idx++] = 'Z';
  return idx - offset;
}
 
Example 5
Source File: OffsetDateTimeExtractYearMonthDayIntegerValues.java    From tutorials with MIT License 4 votes vote down vote up
int getYear(OffsetDateTime offsetDateTime) {
    return offsetDateTime.getYear();
}