List of usage examples for org.joda.time DateTime hourOfDay
public Property hourOfDay()
From source file:com.splicemachine.db.iapi.types.TruncateFunctionUtil.java
License:Apache License
private static DateTimeDataValue doTrunc(DataValueDescriptor dateVal, DataValueDescriptor truncValue, DateTimeDataValue truncd, boolean isTimestamp) throws StandardException { if (dateVal.isNull()) { return truncd; }/*www. j a v a2 s. co m*/ DateTime realDT = dateVal.getDateTime(); DateTime newDT; // DateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond) switch (getTruncValue(truncValue)) { case YEAR: case YR: newDT = realDT.year().roundFloorCopy(); break; case MONTH: case MON: case MO: newDT = realDT.monthOfYear().roundFloorCopy(); break; case DAY: newDT = realDT.dayOfMonth().roundFloorCopy(); break; case HOUR: case HR: if (!isTimestamp) { throw StandardException.newException(SQLState.LANG_TRUNCATE_WRONG_TRUNC_VALUE_FOR_DATE, getTruncValue(truncValue).name()); } newDT = realDT.hourOfDay().roundFloorCopy(); break; case MINUTE: case MIN: if (!isTimestamp) { throw StandardException.newException(SQLState.LANG_TRUNCATE_WRONG_TRUNC_VALUE_FOR_DATE, getTruncValue(truncValue).name()); } newDT = realDT.minuteOfHour().roundFloorCopy(); break; case SECOND: case SEC: if (!isTimestamp) { throw StandardException.newException(SQLState.LANG_TRUNCATE_WRONG_TRUNC_VALUE_FOR_DATE, getTruncValue(truncValue).name()); } newDT = realDT.secondOfMinute().roundFloorCopy(); break; case MILLISECOND: case MILLI: if (!isTimestamp) { throw StandardException.newException(SQLState.LANG_TRUNCATE_WRONG_TRUNC_VALUE_FOR_DATE, getTruncValue(truncValue).name()); } newDT = realDT; break; default: throw StandardException.newException(SQLState.LANG_TRUNCATE_UNKNOWN_TRUNC_VALUE, (isTimestamp ? "TIMESTAMP" : "DATE"), getTruncValue(truncValue), stringifyValues(DateTruncValue.values())); } truncd.setValue(newDT); return truncd; }
From source file:com.sqewd.os.maracache.api.utils.TimeUtils.java
License:Apache License
/** * Get the time bucket for the input date based on the Time Unit and Unit multiplier specified. * * @param dt - Date time to bucket. * @param unit - Time Unit/*w ww .jav a 2 s . com*/ * @param multiplier - Unit multiplier. * @return */ public static DateTime bucket(DateTime dt, TimeUnit unit, int multiplier) { DateTime w = null; switch (unit) { case MILLISECONDS: int ms = (dt.getMillisOfSecond() / multiplier) * multiplier; w = dt.secondOfMinute().roundFloorCopy().plusMillis(ms); break; case SECONDS: int s = (dt.getSecondOfMinute() / multiplier) * multiplier; w = dt.minuteOfHour().roundFloorCopy().plusSeconds(s); break; case MINUTES: int m = (dt.getMinuteOfHour() / multiplier) * multiplier; w = dt.hourOfDay().roundFloorCopy().plusMinutes(m); break; case HOURS: int h = (dt.getHourOfDay() / multiplier) * multiplier; w = dt.dayOfYear().roundFloorCopy().plusHours(h); break; case DAYS: int d = (dt.getDayOfYear() / multiplier) * multiplier; // Need to subtract (1) as the start offset i if (dt.getDayOfYear() % multiplier == 0) { d -= 1; } w = dt.yearOfCentury().roundFloorCopy().plusDays(d); break; } return w; }
From source file:com.squarespace.template.plugins.PluginDateUtils.java
License:Apache License
/** * Takes a strftime()-compatible format string and outputs the properly formatted date. *///from www. ja v a 2s .c o m public static void formatDate(Locale locale, String fmt, long instant, String tzName, StringBuilder buf) { DateTimeZone zone = null; try { zone = DateTimeZone.forID(tzName); } catch (IllegalArgumentException e) { zone = DateTimeZone.getDefault(); } DateTime date = new DateTime(instant, zone); int index = 0; int len = fmt.length(); while (index < len) { char c1 = fmt.charAt(index); index++; if (c1 != '%' || index == len) { buf.append(c1); continue; } char c2 = fmt.charAt(index); switch (c2) { case 'A': buf.append(date.dayOfWeek().getAsText(locale)); break; case 'a': buf.append(date.dayOfWeek().getAsShortText(locale)); break; case 'B': buf.append(date.monthOfYear().getAsText(locale)); break; case 'b': buf.append(date.monthOfYear().getAsShortText(locale)); break; case 'C': leftPad(date.centuryOfEra().get(), '0', 2, buf); break; case 'c': formatAggregate(DateTimeAggregate.FULL, locale, date, buf); break; case 'D': formatAggregate(DateTimeAggregate.MMDDYY, locale, date, buf); break; case 'd': leftPad(date.dayOfMonth().get(), '0', 2, buf); break; case 'e': leftPad(date.dayOfMonth().get(), ' ', 2, buf); break; case 'F': formatAggregate(DateTimeAggregate.YYYYMMDD, locale, date, buf); break; case 'G': buf.append(date.year().get()); break; case 'g': leftPad(date.yearOfCentury().get(), '0', 2, buf); break; case 'H': leftPad(date.hourOfDay().get(), '0', 2, buf); break; case 'h': buf.append(date.monthOfYear().getAsShortText(locale)); break; case 'I': leftPad(date.get(DateTimeFieldType.clockhourOfHalfday()), '0', 2, buf); break; case 'j': leftPad(date.dayOfYear().get(), '0', 3, buf); break; case 'k': leftPad(date.get(DateTimeFieldType.clockhourOfDay()), ' ', 2, buf); break; case 'l': leftPad(date.get(DateTimeFieldType.clockhourOfHalfday()), ' ', 2, buf); break; case 'M': leftPad(date.minuteOfHour().get(), '0', 2, buf); break; case 'm': leftPad(date.monthOfYear().get(), '0', 2, buf); break; case 'n': buf.append('\n'); break; case 'P': buf.append(date.get(DateTimeFieldType.halfdayOfDay()) == 0 ? "am" : "pm"); break; case 'p': buf.append(date.get(DateTimeFieldType.halfdayOfDay()) == 0 ? "AM" : "PM"); break; case 'R': formatAggregate(DateTimeAggregate.H240_M0, locale, date, buf); break; case 'S': leftPad(date.secondOfMinute().get(), '0', 2, buf); break; case 's': buf.append(instant / 1000); break; case 't': buf.append('\t'); break; case 'T': // Equivalent of %H:%M:%S formatAggregate(DateTimeAggregate.H240_M0, locale, date, buf); buf.append(':'); leftPad(date.secondOfMinute().get(), '0', 2, buf); break; case 'U': // TODO: fix week-of-year number leftPad(date.weekOfWeekyear().get(), '0', 2, buf); break; case 'u': buf.append(date.dayOfWeek().get()); break; case 'V': // TODO: fix week-of-year number leftPad(date.weekOfWeekyear().get(), '0', 2, buf); break; case 'v': // Equivalent of %e-%b-%Y leftPad(date.dayOfMonth().get(), ' ', 2, buf); buf.append('-'); buf.append(date.monthOfYear().getAsShortText()); buf.append('-'); buf.append(date.getYear()); break; case 'W': // TODO: fix week-of-year number break; case 'w': buf.append(date.dayOfWeek().get()); break; case 'X': formatAggregate(DateTimeAggregate.HHMMSSP, locale, date, buf); break; case 'x': formatAggregate(DateTimeAggregate.MMDDYYYY, locale, date, buf); break; case 'Y': buf.append(date.getYear()); break; case 'y': leftPad(date.getYearOfCentury(), '0', 2, buf); break; case 'Z': // Note: Joda's nameKey happens to be the same as the shortName. Making // this change to workaround Joda https://github.com/JodaOrg/joda-time/issues/288 buf.append(zone.getNameKey(date.getMillis())); break; case 'z': int offset = date.getZone().getOffset(instant) / 60000; int hours = (int) Math.floor(offset / 60); int minutes = (hours * 60) - offset; if (offset < 0) { buf.append('-'); } leftPad(Math.abs(hours), '0', 2, buf); leftPad(Math.abs(minutes), '0', 2, buf); break; default: // no match, emit literals. buf.append(c1).append(c2); } index++; } }
From source file:com.thinkbiganalytics.ingest.GetTableDataSupport.java
License:Apache License
protected static Date maxAllowableDateFromUnit(Date fromDate, UnitSizes unit) { DateTime jodaDate = new DateTime(fromDate); switch (unit) { case HOUR:/* w w w . j a va2 s .c o m*/ return jodaDate.hourOfDay().roundFloorCopy().toDate(); case DAY: return jodaDate.withHourOfDay(0).hourOfDay().roundFloorCopy().toDate(); case WEEK: return jodaDate.weekOfWeekyear().roundFloorCopy().toDate(); case MONTH: return jodaDate.monthOfYear().roundFloorCopy().toDate(); case YEAR: return jodaDate.withMonthOfYear(1).withDayOfMonth(1).withHourOfDay(0).hourOfDay().roundFloorCopy() .toDate(); } return fromDate; }
From source file:com.tikal.tallerWeb.modelo.alerta.RangoAlertaVerificacion.java
License:Apache License
private DateTime armaFechaRangoIzquierdo(int mes, int yearSwift) { DateTime r = new DateTime(); r = new DateTime(r.getYear() + yearSwift, mes, r.dayOfMonth().getMinimumValue(), r.hourOfDay().getMinimumValue(), r.minuteOfHour().getMinimumValue(), r.secondOfMinute().getMinimumValue(), r.millisOfSecond().getMinimumValue(), r.getZone()); r = r.minusDays(diasAntesDelMes);/*from w ww.ja v a2s . c om*/ return r; }
From source file:com.tikal.tallerWeb.modelo.alerta.RangoAlertaVerificacion.java
License:Apache License
private DateTime armaFechaRangoDerecho(int mes, int yearSwift) { DateTime r = new DateTime(); r = new DateTime(r.getYear() + yearSwift, mes, r.dayOfMonth().getMinimumValue(), r.hourOfDay().getMinimumValue(), r.minuteOfHour().getMinimumValue(), r.secondOfMinute().getMinimumValue(), r.millisOfSecond().getMinimumValue(), r.getZone()); r = new DateTime(r.getYear(), mes, r.dayOfMonth().getMaximumValue(), r.hourOfDay().getMaximumValue(), r.minuteOfHour().getMaximumValue(), r.secondOfMinute().getMaximumValue(), r.millisOfSecond().getMaximumValue(), r.getZone()); return r;//from www .ja v a 2 s. c o m }
From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java
License:BSD License
public static String getTemporalStr(int temporalResolution, int time) { DateTime dt = new DateTime(((long) time) * 1000, DateTimeZone.UTC); String temporal = ""; switch (temporalResolution) { case HOUR:/*w ww. jav a 2s . co m*/ temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString() + "-" + dt.dayOfMonth().getAsString() + "-" + dt.hourOfDay().getAsString(); break; case DAY: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString() + "-" + dt.dayOfMonth().getAsString(); break; case WEEK: temporal = dt.weekyear().getAsString() + "-" + dt.weekOfWeekyear().getAsString(); break; case MONTH: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString(); break; case YEAR: temporal = dt.year().getAsString(); break; default: temporal = null; break; } return temporal; }
From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java
License:BSD License
public static String getTemporalStr(int temporalResolution, DateTime dt) { String temporal = ""; switch (temporalResolution) { case HOUR:/*from w ww .ja v a 2 s . c o m*/ temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString() + "-" + dt.dayOfMonth().getAsString() + "-" + dt.hourOfDay().getAsString(); break; case DAY: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString() + "-" + dt.dayOfMonth().getAsString(); break; case WEEK: temporal = dt.weekyear().getAsString() + "-" + dt.weekOfWeekyear().getAsString(); break; case MONTH: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString(); break; case YEAR: temporal = dt.year().getAsString(); break; default: temporal = null; break; } return temporal; }
From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java
License:BSD License
public static int getTime(int temporalResolution, String[] input, int tempPosition) { String temporal = ""; String format = ""; long time = 0L; try {/* w w w .j a v a 2 s . c om*/ time = ((long) Double.parseDouble(input[tempPosition])) * 1000; } catch (Exception e) { return -1; } if (time < 0) return -1; DateTime dt = new DateTime(time, DateTimeZone.UTC); switch (temporalResolution) { case HOUR: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString() + "-" + dt.dayOfMonth().getAsString() + "-" + dt.hourOfDay().getAsString(); format = "yyyy-MM-dd-HH z"; break; case DAY: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString() + "-" + dt.dayOfMonth().getAsString(); format = "yyyy-MM-dd z"; break; case WEEK: temporal = dt.weekyear().getAsString() + "-" + dt.weekOfWeekyear().getAsString(); format = "xxxx-ww z"; break; case MONTH: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString(); format = "yyyy-MM z"; break; case YEAR: temporal = dt.year().getAsString(); format = "yyyy z"; break; default: temporal = null; format = ""; break; } if (temporal == null) return -1; DateTimeFormatter formatter = DateTimeFormat.forPattern(format); return (int) (formatter.parseDateTime(temporal + " UTC").getMillis() / 1000); }
From source file:edu.nyu.vida.data_polygamy.utils.FrameworkUtils.java
License:BSD License
public static int getTime(int temporalResolution, int[] input, int tempPosition) { String temporal = ""; String format = ""; long time = 0L; try {/*from w w w . j av a2 s.c o m*/ time = ((long) input[tempPosition]) * 1000; } catch (Exception e) { return -1; } DateTime dt = new DateTime(time, DateTimeZone.UTC); switch (temporalResolution) { case HOUR: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString() + "-" + dt.dayOfMonth().getAsString() + "-" + dt.hourOfDay().getAsString(); format = "yyyy-MM-dd-HH z"; break; case DAY: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString() + "-" + dt.dayOfMonth().getAsString(); format = "yyyy-MM-dd z"; break; case WEEK: temporal = dt.weekyear().getAsString() + "-" + dt.weekOfWeekyear().getAsString(); format = "xxxx-ww z"; break; case MONTH: temporal = dt.year().getAsString() + "-" + dt.monthOfYear().getAsString(); format = "yyyy-MM z"; break; case YEAR: temporal = dt.year().getAsString(); format = "yyyy z"; break; default: temporal = null; format = ""; break; } if (temporal == null) return -1; DateTimeFormatter formatter = DateTimeFormat.forPattern(format); return (int) (formatter.parseDateTime(temporal + " UTC").getMillis() / 1000); }