/** * Returns the day number since the epoch date. * @param date the target date * @return the day number * @since 0.7.4 * @see #toElapsedDays(int) */ public static int getDaysSinceEpoch(Date date) { return date.getElapsedDays() - DATE_EPOCH_OFFSET; }
/** * Returns the value which this object represents. * @param alternate the alternative value for {@code null} * @return the value which this object represents, or the alternative one if this object represents {@code null} */ public int or(int alternate) { if (nullValue) { return alternate; } return get().getElapsedDays(); }
@Override public void write(DataOutput out) throws IOException { if (isNull()) { out.writeBoolean(false); } else { out.writeBoolean(true); out.writeInt(entity.getElapsedDays()); } }
/** * Creates a new instance which represents the specified value. * @param valueOrNull the initial value (nullable) */ public DateOption(Date valueOrNull) { if (valueOrNull == null) { this.nullValue = true; } else { this.entity.setElapsedDays(valueOrNull.getElapsedDays()); this.nullValue = false; } }
final void setValue(Date value) { DateUtil.setDayToCalendar(value.getElapsedDays(), calendarBuffer); representation.set(format.format(calendarBuffer.getTime())); }
/** * Sets the value. * @param newValue the value (nullable) * @return this * @deprecated Application developer should not use this method directly */ @Deprecated public DateOption modify(Date newValue) { if (newValue == null) { this.nullValue = true; } else { this.nullValue = false; this.entity.setElapsedDays(newValue.getElapsedDays()); } return this; }
/** * Returns the nano time of the julian day. * @param date the day * @return the nano time of julian day * @since 0.7.2 */ public static long getTimeOfDayNanos(Date date) { long julianSeconds = toJulianSecond(TimeUnit.DAYS.toSeconds(date.getElapsedDays())); return TimeUnit.SECONDS.toNanos(DateUtil.getSecondOfDay(julianSeconds)); }
/** * Returns the Julian day number of the date. * @param date the date * @return the Julian day number * @since 0.7.2 */ public static int getJulianDayNumber(Date date) { long julianSeconds = toJulianSecond(TimeUnit.DAYS.toSeconds(date.getElapsedDays())); return getJulianDayNumber(julianSeconds); }
@Override protected String toString(Object value) { DateOption option = (DateOption) value; return TemporalUtil.toDateString(option.get().getElapsedDays()); } }
@Override protected String toString(ValueOption<?> object) { return TemporalUtil.toDateString(((DateOption) object).get().getElapsedDays()); } }
@Override protected void doEmit(DateOption property, StringBuilder output) { formatter.emit(property.get().getElapsedDays(), output); }
@Override protected void doWrite(DateOption property, ValueWriter writer) throws IOException { buffer.setLength(0); formatter.emit(property.get().getElapsedDays(), buffer); writer.writeString(buffer); }
@Override public DateWritable getPrimitiveWritableObject(Object o) { DateOption object = (DateOption) o; if (object == null || object.isNull()) { return null; } Date value = object.get(); return new DateWritable(value.getElapsedDays() - EPOCH_OFFSET); } }
@Override public void emit(DateOption option) throws IOException { startCell(); if (emitNull(option)) { return; } int days = option.get().getElapsedDays(); emitDate(days); }
@Override public void emit(DateOption option) throws IOException { addCellDelimiter(); if (option.isNull() == false) { CharSequence string = dateFormat.format(option.get().getElapsedDays()); if (escapeDate || isEscapeTarget()) { appendEscaped(lineBuffer, string); } else { lineBuffer.append(string); } } }
void checkCalendar(int start, int end) { GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT")); calendar.clear(); calendar.set(Calendar.YEAR, start); calendar.set(Calendar.MONTH, Calendar.JANUARY); calendar.set(Calendar.DATE, 1); Date date = new Date(); date.setElapsedDays(DateUtil.getDayFromDate(start, 1, 1)); while (calendar.get(Calendar.YEAR) <= end) { String calString = calendar.toString(); assertThat( "Year: " + calString, date.getYear(), is(calendar.get(Calendar.YEAR))); assertThat( "Month: " + calString, date.getMonth(), is(calendar.get(Calendar.MONTH) + 1)); assertThat( "Date: " + calString, date.getDay(), is(calendar.get(Calendar.DATE))); calendar.add(Calendar.DATE, 1); date.setElapsedDays(date.getElapsedDays() + 1); } } }
/** * test for set elapsed days. */ @Test public void setInt() { DateOption option = new DateOption(); option.modify(500); assertThat(option.isNull(), is(false)); assertThat(option.get().getElapsedDays(), is(500)); }