public static Date getRandDate(Random r) { String dateStr = String.format("%d-%02d-%02d", Integer.valueOf(1800 + r.nextInt(500)), // year Integer.valueOf(1 + r.nextInt(12)), // month Integer.valueOf(1 + r.nextInt(28))); // day Date dateVal = Date.valueOf(dateStr); return dateVal; }
@Override public String vectorExpressionParameters() { Date dt = Date.ofEpochMilli(DateWritableV2.daysToMillis((int) value)); return "date " + dt.toString() + ", " + getColumnParamString(0, colNum); }
@Override public String vectorExpressionParameters() { String value; if (object instanceof Long) { Date tempDate = new Date(); tempDate.setTimeInMillis(DateWritableV2.daysToMillis((int) longValue)); value = tempDate.toString(); } else if (object instanceof Timestamp) { value = org.apache.hadoop.hive.common.type.Timestamp.ofEpochMilli( timestampValue.getTime(), timestampValue.getNanos()).toString(); } else if (object instanceof byte []) { value = new String(this.stringValue, StandardCharsets.UTF_8); } else { value = "unknown"; } return "val " + value + ", " + getColumnParamString(0, colNum); }
@Override Date transform(final Date value) { int actualMonthValue = maskedMonthValue + 1; int year = maskedYearValue == UNMASKED_VAL ? value.getYear() : maskedYearValue; int month = maskedMonthValue == UNMASKED_VAL ? value.getMonth() : actualMonthValue; int day = maskedDayValue == UNMASKED_VAL ? value.getDay() : maskedDayValue; return Date.of(year, month, day); }
public static long daysToMillis(int days) { return Date.ofEpochDay(days).toEpochMilli(); }
public boolean parseDate(String strValue, Date result) { Date parsedVal; try { parsedVal = Date.valueOf(strValue); } catch (IllegalArgumentException e) { parsedVal = null; } if (parsedVal == null) { return false; } result.setTimeInMillis(parsedVal.toEpochMilli()); return true; } }
/** * Get the week of the year from a date string. * * @param dateString * the dateString in the format of "yyyy-MM-dd HH:mm:ss" or * "yyyy-MM-dd". * @return an int from 1 to 53. null if the dateString is not a valid date * string. */ public IntWritable evaluate(Text dateString) { if (dateString == null) { return null; } try { Date date = Date.valueOf(dateString.toString()); calendar.setTimeInMillis(date.toEpochMilli()); result.set(calendar.get(Calendar.WEEK_OF_YEAR)); return result; } catch (IllegalArgumentException e) { return null; } }
/** * Write DATE. * The representation of date in Teradata binary format is: * The Date D is a int with 4 bytes using little endian. * The representation is (YYYYMMDD - 19000000).toInt -> D * eg. 1911.11.11 -> 19111111 -> 111111 -> 07 b2 01 00 in little endian. * the null date will use 0 to pad. * * @param date the date * @throws IOException the io exception */ public void writeDate(DateWritableV2 date) throws IOException { if (date == null) { EndianUtils.writeSwappedInteger(this, 0); return; } int toWrite = date.get().getYear() * 10000 + date.get().getMonth() * 100 + date.get().getDay() - 19000000; EndianUtils.writeSwappedInteger(this, toWrite); }
@Override public String toString() { return date.toString(); }
/** * Return a copy of this object. */ public Object clone() { // LocalDateTime is immutable. return new Date(this.localDate); }
public boolean add(HiveIntervalYearMonth interval, Date dt, Date result) { if (dt == null || interval == null) { return false; } long resultMillis = addMonthsToMillis(dt.toEpochMilli(), interval.getTotalMonths()); result.setTimeInMillis(resultMillis); return true; }
protected void evaluateString(ColumnVector columnVector, LongColumnVector outputVector, int i) { BytesColumnVector bcv = (BytesColumnVector) columnVector; text.set(bcv.vector[i], bcv.start[i], bcv.length[i]); org.apache.hadoop.hive.common.type.Date hDate = new org.apache.hadoop.hive.common.type.Date(); boolean parsed = dateParser.parseDate(text.toString(), hDate); if (!parsed) { outputVector.noNulls = false; outputVector.isNull[i] = true; return; } long days = DateWritableV2.millisToDays(hDate.toEpochMilli()); if (isPositive) { days += numDays; } else { days -= numDays; } outputVector.vector[i] = days; }
private Date evalDate(Date d) throws UDFArgumentException { date.setTimeInDays(d.toEpochDay()); if ("MONTH".equals(fmtInput) || "MON".equals(fmtInput) || "MM".equals(fmtInput)) { date.setDayOfMonth(1); return date; } else if ("QUARTER".equals(fmtInput) || "Q".equals(fmtInput)) { int month = date.getMonth() - 1; int quarter = month / 3; int monthToSet = quarter * 3 + 1; date.setMonth(monthToSet); date.setDayOfMonth(1); return date; } else if ("YEAR".equals(fmtInput) || "YYYY".equals(fmtInput) || "YY".equals(fmtInput)) { date.setMonth(1); date.setDayOfMonth(1); return date; } else { return null; } }
@Deprecated public Object create(java.sql.Date value) { return Date.ofEpochMilli(value.getTime()); }
private Calendar addMonth(Date d, int numMonths) { calendar.setTimeInMillis(d.toEpochMilli()); return addMonth(numMonths); }
org.apache.hadoop.hive.common.type.Date date = new org.apache.hadoop.hive.common.type.Date(); date.setTimeInMillis(d.getTime()); return new DateWritableV2(date);
/** * Perform date + int operation . * @param dt the date * @param interval the int (days) * @return the resulting date */ public Date add(Date dt, int interval) { if (dt == null) { return null; } Date dtResult = new Date(); dtResult.setTimeInDays(dt.toEpochDay() + interval); return dtResult; }
public void testDate() throws HiveException { GenericUDFToUnixTimeStamp udf = new GenericUDFToUnixTimeStamp(); ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableDateObjectInspector; ObjectInspector[] arguments = {valueOI}; udf.initialize(arguments); Date date = Date.valueOf("1970-01-01"); runAndVerify(udf, new DateWritableV2(date), new LongWritable(date.toEpochSecond())); // test null values runAndVerify(udf, null, null); }
/** * Read DATE. * The representation of date in Teradata binary format is: * The Date D is a int with 4 bytes using little endian, * The representation is (D+19000000).ToString -> YYYYMMDD, * eg: Date 07 b2 01 00 -> 111111 in little endian -> 19111111 - > 1911.11.11. * the null date will use 0 to pad. * * @return the date * @throws IOException the io exception * @throws ParseException the parse exception */ public Date readDate() throws IOException, ParseException { int di = readInt(); if (di == 0) { return null; } String dateString = String.valueOf(di + 19000000); if (dateString.length() < DATE_STRING_LENGTH) { dateString = StringUtils.leftPad(dateString, DATE_STRING_LENGTH, '0'); } Date date = new Date(); date.setYear(Integer.parseInt(dateString.substring(0, 4))); date.setMonth(Integer.parseInt(dateString.substring(4, 6))); date.setDayOfMonth(Integer.parseInt(dateString.substring(6, 8))); return date; }