private static long year(final LocalDateTime localDateTime) { return localDateTime.getYear() * 100000000L; }
protected Calendar dateToCalendar(Date date, TimeZone offerTimeZone) { DateTimeZone offerDateTimeZone = DateTimeZone.forTimeZone(offerTimeZone); LocalDateTime offerDateTime = new LocalDateTime(date, offerDateTimeZone); Calendar calendar = new GregorianCalendar(offerTimeZone); calendar.set(Calendar.YEAR, offerDateTime.getYear()); calendar.set(Calendar.MONTH, offerDateTime.getMonthOfYear() - 1); calendar.set(Calendar.DAY_OF_MONTH, offerDateTime.getDayOfMonth()); calendar.set(Calendar.HOUR_OF_DAY, offerDateTime.getHourOfDay()); calendar.set(Calendar.MINUTE, offerDateTime.getMinuteOfHour()); calendar.set(Calendar.SECOND, offerDateTime.getSecondOfMinute()); calendar.get(Calendar.HOUR_OF_DAY);//do not delete this line calendar.get(Calendar.MINUTE); return calendar; }
/** * Get the date time as a <code>java.util.Date</code> using the specified time zone. * <p> * The <code>Date</code> object created has exactly the same fields as this * date-time, except when the time would be invalid due to a daylight savings * gap. In that case, the time will be set to the earliest valid time after the gap. * <p> * In the case of a daylight savings overlap, the earlier instant is selected. * <p> * Converting to a JDK Date is full of complications as the JDK Date constructor * doesn't behave as you might expect around DST transitions. This method works * by taking a first guess and then adjusting. This also handles the situation * where the JDK time zone data differs from the Joda-Time time zone data. * <p> * Unlike {@link #toDate()}, this implementation does not rely on Java's synchronized * time zone initialization logic, and should demonstrate better concurrent performance * characteristics. * * @return a Date initialised with this date-time, never null * @since 2.3 */ public Date toDate(final TimeZone timeZone) { final Calendar calendar = Calendar.getInstance(timeZone); calendar.clear(); calendar.set(getYear(), getMonthOfYear() - 1, getDayOfMonth(), getHourOfDay(), getMinuteOfHour(), getSecondOfMinute()); Date date = calendar.getTime(); date.setTime(date.getTime() + getMillisOfSecond()); return correctDstTransition(date, timeZone); }
/** * Get the date time as a <code>java.util.Date</code> using the specified time zone. * <p> * The <code>Date</code> object created has exactly the same fields as this * date-time, except when the time would be invalid due to a daylight savings * gap. In that case, the time will be set to the earliest valid time after the gap. * <p> * In the case of a daylight savings overlap, the earlier instant is selected. * <p> * Converting to a JDK Date is full of complications as the JDK Date constructor * doesn't behave as you might expect around DST transitions. This method works * by taking a first guess and then adjusting. This also handles the situation * where the JDK time zone data differs from the Joda-Time time zone data. * <p> * Unlike {@link #toDate()}, this implementation does not rely on Java's synchronized * time zone initialization logic, and should demonstrate better concurrent performance * characteristics. * * @return a Date initialised with this date-time, never null * @since 2.3 */ public Date toDate(final TimeZone timeZone) { final Calendar calendar = Calendar.getInstance(timeZone); calendar.clear(); calendar.set(getYear(), getMonthOfYear() - 1, getDayOfMonth(), getHourOfDay(), getMinuteOfHour(), getSecondOfMinute()); Date date = calendar.getTime(); date.setTime(date.getTime() + getMillisOfSecond()); return correctDstTransition(date, timeZone); }
/** * Get the date time as a <code>java.util.Date</code>. * <p> * The <code>Date</code> object created has exactly the same fields as this * date-time, except when the time would be invalid due to a daylight savings * gap. In that case, the time will be set to the earliest valid time after the gap. * <p> * In the case of a daylight savings overlap, the earlier instant is selected. * <p> * Converting to a JDK Date is full of complications as the JDK Date constructor * doesn't behave as you might expect around DST transitions. This method works * by taking a first guess and then adjusting. This also handles the situation * where the JDK time zone data differs from the Joda-Time time zone data. * * @return a Date initialised with this date-time, never null * @since 2.0 */ @SuppressWarnings("deprecation") public Date toDate() { int dom = getDayOfMonth(); Date date = new Date(getYear() - 1900, getMonthOfYear() - 1, dom, getHourOfDay(), getMinuteOfHour(), getSecondOfMinute()); date.setTime(date.getTime() + getMillisOfSecond()); return correctDstTransition(date, TimeZone.getDefault()); }
/** * Get the date time as a <code>java.util.Date</code>. * <p> * The <code>Date</code> object created has exactly the same fields as this * date-time, except when the time would be invalid due to a daylight savings * gap. In that case, the time will be set to the earliest valid time after the gap. * <p> * In the case of a daylight savings overlap, the earlier instant is selected. * <p> * Converting to a JDK Date is full of complications as the JDK Date constructor * doesn't behave as you might expect around DST transitions. This method works * by taking a first guess and then adjusting. This also handles the situation * where the JDK time zone data differs from the Joda-Time time zone data. * * @return a Date initialised with this date-time, never null * @since 2.0 */ @SuppressWarnings("deprecation") public Date toDate() { int dom = getDayOfMonth(); Date date = new Date(getYear() - 1900, getMonthOfYear() - 1, dom, getHourOfDay(), getMinuteOfHour(), getSecondOfMinute()); date.setTime(date.getTime() + getMillisOfSecond()); return correctDstTransition(date, TimeZone.getDefault()); }
private Object getRandomValueForTimeColumn(boolean isSimpleDate) { long randomMs = ThreadLocalRandom.current().nextLong(startTime); long dateColVal = randomMs; Object result; if (isSimpleDate) { DateTime dateTime = new DateTime(randomMs); LocalDateTime localDateTime = dateTime.toLocalDateTime(); int year = localDateTime.getYear(); int month = localDateTime.getMonthOfYear(); int day = localDateTime.getDayOfMonth(); String dateColStr = String.format("%04d%02d%02d", year, month, day); dateColVal = Integer.valueOf(dateColStr); result = new Integer(Integer.valueOf(dateColStr)); } else { result = new Long(dateColVal); } if (dateColVal < minTime) { minTime = dateColVal; } if (dateColVal > maxTime) { maxTime = dateColVal; } return result; }
/** * Converts this object to a DateTime using the specified zone. * <p> * When the time zone is applied, the local date-time may be affected by daylight saving. * In a daylight saving gap, when the local time does not exist, * this method will throw an exception. * In a daylight saving overlap, when the same local time occurs twice, * this method returns the first occurrence of the local time. * * @param zone time zone to apply, or default if null * @return a DateTime using the same millis * @throws IllegalInstantException if the local time does not exist when the time zone is applied */ public DateTime toDateTime(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); Chronology chrono = iChronology.withZone(zone); return new DateTime( getYear(), getMonthOfYear(), getDayOfMonth(), getHourOfDay(), getMinuteOfHour(), getSecondOfMinute(), getMillisOfSecond(), chrono); }
/** * Converts this object to a DateTime using the specified zone. * <p> * When the time zone is applied, the local date-time may be affected by daylight saving. * In a daylight saving gap, when the local time does not exist, * this method will throw an exception. * In a daylight saving overlap, when the same local time occurs twice, * this method returns the first occurrence of the local time. * * @param zone time zone to apply, or default if null * @return a DateTime using the same millis * @throws IllegalInstantException if the local time does not exist when the time zone is applied */ public DateTime toDateTime(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); Chronology chrono = iChronology.withZone(zone); return new DateTime( getYear(), getMonthOfYear(), getDayOfMonth(), getHourOfDay(), getMinuteOfHour(), getSecondOfMinute(), getMillisOfSecond(), chrono); }
public Date toDate() { int dom = getDayOfMonth(); Date date = new Date(getYear() - 1900, getMonthOfYear() - 1, dom, getHourOfDay(), getMinuteOfHour(), getSecondOfMinute()); date.setTime(date.getTime() + getMillisOfSecond());
/** * Converts this object to a DateTime using the specified zone. * <p> * This method will throw an exception if the datetime that would be * created does not exist when the time zone is taken into account. * * @param zone time zone to apply, or default if null * @return a DateTime using the same millis */ public DateTime toDateTime(DateTimeZone zone) { zone = DateTimeUtils.getZone(zone); Chronology chrono = iChronology.withZone(zone); return new DateTime( getYear(), getMonthOfYear(), getDayOfMonth(), getHourOfDay(), getMinuteOfHour(), getSecondOfMinute(), getMillisOfSecond(), chrono); }
/** * Returns true if both datetime are in the same year, false otherwise. * * @param actual the actual datetime. expected not be null * @param other the other datetime. expected not be null * @return true if both datetime are in the same year, false otherwise */ private static boolean haveSameYear(LocalDateTime actual, LocalDateTime other) { return actual.getYear() == other.getYear(); } }
@Override public Date getDate(int index) { if (ac.isNull(index)) { return null; } org.joda.time.LocalDateTime date = new org.joda.time.LocalDateTime(ac.get(index), org.joda.time.DateTimeZone.UTC); return new Date(date.getYear() - 1900, date.getMonthOfYear() - 1, date.getDayOfMonth()); }
@Override public java.time.LocalDateTime convert(LocalDateTime in, Context context) throws Exception { if (in == null) return null; return java.time.LocalDateTime.of(in.getYear(), in.getMonthOfYear(), in.getDayOfMonth(), in.getHourOfDay(), in.getMinuteOfHour(), in.getSecondOfMinute(), in.getMillisOfSecond() * 1000); } }
@Override public Timestamp getTimestamp(int index) { if (ac.isNull(index)) { return null; } org.joda.time.LocalDateTime date = new org.joda.time.LocalDateTime(ac.get(index), org.joda.time.DateTimeZone.UTC); return new Timestamp(date.getYear() - 1900, date.getMonthOfYear() - 1, date.getDayOfMonth(), date.getHourOfDay(), date.getMinuteOfHour(), date.getSecondOfMinute(), (int) java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(date.getMillisOfSecond())); }
@Override public void setup() { int timeZoneIndex = contextInfo.getRootFragmentTimeZone(); org.joda.time.DateTimeZone timeZone = org.joda.time.DateTimeZone.forID(org.apache.arrow.vector.util.DateUtility.getTimeZone(timeZoneIndex)); org.joda.time.LocalDateTime now = new org.joda.time.LocalDateTime(contextInfo.getQueryStartTime(), timeZone); queryStartDate = (new org.joda.time.DateMidnight(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), timeZone)). withZoneRetainFields(org.joda.time.DateTimeZone.UTC).getMillis(); }
@Override public void setup() { org.joda.time.LocalDateTime now = new org.joda.time.LocalDateTime(contextInfo.getQueryStartTime(), org.joda.time.DateTimeZone.UTC); queryStartDate = (new org.joda.time.DateMidnight(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), org.joda.time.DateTimeZone.UTC)). withZoneRetainFields(org.joda.time.DateTimeZone.UTC).getMillis(); }
@Override public void write(Kryo kryo, Output output, LocalDateTime localDateTime) { final int packedYearMonthDay = localDateTime.getYear() * 13 * 32 + localDateTime.getMonthOfYear() * 32 + localDateTime.getDayOfMonth(); output.writeLong((long)packedYearMonthDay * 86400000 + localDateTime.getMillisOfDay(), true); final String chronologyId = IdentifiableChronology.getChronologyId(localDateTime.getChronology()); output.writeString(chronologyId == null ? "" : chronologyId); } }
@Override public void write(Kryo kryo, Output output, LocalDateTime localDateTime) { final int packedYearMonthDay = localDateTime.getYear() * 13 * 32 + localDateTime.getMonthOfYear() * 32 + localDateTime.getDayOfMonth(); output.writeLong((long)packedYearMonthDay * 86400000 + localDateTime.getMillisOfDay(), true); final String chronologyId = IdentifiableChronology.getChronologyId(localDateTime.getChronology()); output.writeString(chronologyId == null ? "" : chronologyId); } }
@Override public RexNode convertCall(SqlRexContext cx, SqlCall call) { final int timeZoneIndex = getContextInformation().getRootFragmentTimeZone(); final DateTimeZone timeZone = DateTimeZone.forID(DateUtility.getTimeZone(timeZoneIndex)); final LocalDateTime dateTime = new LocalDateTime(getContextInformation().getQueryStartTime(), timeZone); final long midNightAsMillis = new DateMidnight(dateTime.getYear(), dateTime.getMonthOfYear(), dateTime.getDayOfMonth(), timeZone) .withZoneRetainFields(DateTimeZone.UTC) .getMillis(); return cx.getRexBuilder() .makeDateLiteral(DateTimes.toDateTime( new LocalDateTime(midNightAsMillis, DateTimeZone.UTC)) .toCalendar(null)); // null sets locale to default locale } }