下面列出了java.time.OffsetDateTime#getYear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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");
}
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();
}
}
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;
}
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;
}
int getYear(OffsetDateTime offsetDateTime) {
return offsetDateTime.getYear();
}