List of usage examples for org.joda.time DateTime dayOfYear
public Property dayOfYear()
From source file:com.chiorichan.plugin.lua.api.OSAPI.java
License:Mozilla Public License
@Override void initialize() { // Push a couple of functions that override original Lua API functions or // that add new functionality to it. lua.getGlobal("os"); // Custom os.clock() implementation returning the time the computer has // been actively running, instead of the native library... lua.pushJavaFunction(new JavaFunction() { @Override/*from w w w .j ava 2 s . co m*/ public int invoke(LuaState lua) { lua.pushNumber(System.currentTimeMillis()); return 1; } }); lua.setField(-2, "clock"); lua.pushJavaFunction(new JavaFunction() { @Override public int invoke(LuaState lua) { String format = lua.getTop() > 0 && lua.isString(1) ? lua.toString(1) : "%d/%m/%y %H:%M:%S"; long time = (long) (lua.getTop() > 1 && lua.isNumber(2) ? lua.toNumber(2) * 1000 / 60 / 60 : System.currentTimeMillis()); DateTime dt = new DateTime(time); if (format == "*t") { lua.newTable(0, 8); lua.pushInteger(dt.year().get()); lua.setField(-2, "year"); lua.pushInteger(dt.monthOfYear().get()); lua.setField(-2, "month"); lua.pushInteger(dt.dayOfMonth().get()); lua.setField(-2, "day"); lua.pushInteger(dt.hourOfDay().get()); lua.setField(-2, "hour"); lua.pushInteger(dt.minuteOfHour().get()); lua.setField(-2, "min"); lua.pushInteger(dt.secondOfMinute().get()); lua.setField(-2, "sec"); lua.pushInteger(dt.dayOfWeek().get()); lua.setField(-2, "wday"); lua.pushInteger(dt.dayOfYear().get()); lua.setField(-2, "yday"); } else lua.pushString(dt.toString(DateTimeFormat.forPattern(format))); return 1; } }); lua.setField(-2, "date"); /* * // Date formatting function. * lua.pushScalaFunction(lua => { * val format = * if (lua.getTop > 0 && lua.isString(1)) lua.toString(1) * else "%d/%m/%y %H:%M:%S" * val time = * if (lua.getTop > 1 && lua.isNumber(2)) lua.toNumber(2) * 1000 / 60 / 60 * else machine.worldTime + 5000 * * val dt = GameTimeFormatter.parse(time) * def fmt(format: String) { * if (format == "*t") { * lua.newTable(0, 8) * lua.pushInteger(dt.year) * lua.setField(-2, "year") * lua.pushInteger(dt.month) * lua.setField(-2, "month") * lua.pushInteger(dt.day) * lua.setField(-2, "day") * lua.pushInteger(dt.hour) * lua.setField(-2, "hour") * lua.pushInteger(dt.minute) * lua.setField(-2, "min") * lua.pushInteger(dt.second) * lua.setField(-2, "sec") * lua.pushInteger(dt.weekDay) * lua.setField(-2, "wday") * lua.pushInteger(dt.yearDay) * lua.setField(-2, "yday") * } * else { * lua.pushString(GameTimeFormatter.format(format, dt)) * } * } * * // Just ignore the allowed leading '!', Minecraft has no time zones... * if (format.startsWith("!")) * fmt(format.substring(1)) * else * fmt(format) * 1 * }) * lua.setField(-2, "date") * * // Return ingame time for os.time(). * lua.pushScalaFunction(lua => { * if (lua.isNoneOrNil(1)) { * // Game time is in ticks, so that each day has 24000 ticks, meaning * // one hour is game time divided by one thousand. Also, Minecraft * // starts days at 6 o'clock, versus the 1 o'clock of timestamps so we * // add those five hours. Thus: * // timestamp = (time + 5000) * 60[kh] * 60[km] / 1000[s] * lua.pushNumber((machine.worldTime + 5000) * 60 * 60 / 1000) * } * else { * def getField(key: String, d: Int) = { * lua.getField(-1, key) * val res = lua.toIntegerX(-1) * lua.pop(1) * if (res == null) * if (d < 0) throw new Exception("field '" + key + "' missing in date table") * else d * else res: Int * } * * lua.checkType(1, LuaType.TABLE) * lua.setTop(1) * * val sec = getField("sec", 0) * val min = getField("min", 0) * val hour = getField("hour", 12) * val mday = getField("day", -1) * val mon = getField("month", -1) * val year = getField("year", -1) * * GameTimeFormatter.mktime(year, mon, mday, hour, min, sec) match { * case Some(time) => lua.pushNumber(time) * case _ => lua.pushNil() * } * } * 1 * }) * lua.setField(-2, "time") */ // Pop the os table. lua.pop(1); }
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/*from w ww .j av a2s .co m*/ * @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 ww w . j a va 2 s . com 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.squid.kraken.v4.core.analysis.engine.processor.AnalysisCompute.java
License:Open Source License
private IntervalleObject alignPastInterval(IntervalleObject presentInterval, IntervalleObject pastInterval, Axis joinAxis) throws ScopeException { if (joinAxis != null && presentInterval != null && pastInterval != null) { Object lowerPresent = presentInterval.getLowerBound(); Object lowerPast = pastInterval.getLowerBound(); Object upperPresent = presentInterval.getUpperBound(); Object upperPast = pastInterval.getUpperBound(); ///* www. j av a 2s .co m*/ IDomain image = joinAxis.getDefinition().getImageDomain(); if (lowerPresent instanceof Date && lowerPast instanceof Date) { DateTime lowerPastDT = new DateTime((Date) lowerPast); DateTime lowerPresentDT = new DateTime((Date) lowerPresent); DateTime upperPresentDT = new DateTime((Date) upperPresent); DateTime upperPastDT = new DateTime((Date) upperPast); // realign if (image.isInstanceOf(IDomain.YEARLY)) { // check if present is an exact number of years if (lowerPresentDT.getDayOfYear() == 1 && upperPresentDT.getDayOfYear() == upperPresentDT.dayOfYear().getMaximumValue()) { // check of both periods have the same number of days Period presentPeriod = new Period(new LocalDate(lowerPresent), (new LocalDate(upperPresent)), PeriodType.days()); Period pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)), PeriodType.days()); if (presentPeriod.getDays() == pastPeriod.getDays()) { presentPeriod = new Period(new LocalDate(lowerPresent), (new LocalDate(upperPresent)).plusDays(1), PeriodType.years()); pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)).plusDays(1), PeriodType.years()); // realign if (presentPeriod.getYears() > pastPeriod.getYears()) { // some days are missing to align the periods if (lowerPastDT.getDayOfYear() != 1) { // previous period Date newLowerPast = new DateTime(upperPastDT.getYear(), 1, 1, 0, 0).toDate(); return new IntervalleObject(newLowerPast, upperPast); } if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) { // year over year Date newUpperPast = new DateTime(upperPastDT.getYear(), 12, 31, 23, 59) .toDate(); return new IntervalleObject(lowerPast, newUpperPast); } } else { // either already aligned, or some days should // be removed if (upperPastDT.getDayOfYear() != upperPastDT.dayOfYear().getMaximumValue()) { // year over Year Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59) .toDate(); return new IntervalleObject(lowerPast, newUpperPast); } if (lowerPastDT.getDayOfYear() != 1) { // previous period Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0) .toDate(); return new IntervalleObject(newLowerPast, upperPast); } } } } } else if (image.isInstanceOf(IDomain.QUATERLY) || image.isInstanceOf(IDomain.MONTHLY)) { // check if present is an exact number of month if (lowerPresentDT.getDayOfMonth() == 1 && upperPresentDT.getDayOfMonth() == upperPresentDT.dayOfMonth().getMaximumValue()) { // check of both periods have the same number of days Period presentPeriod = new Period(new LocalDate(lowerPresent), new LocalDate(upperPresent), PeriodType.days()); Period pastPeriod = new Period(new LocalDate(lowerPast), new LocalDate(upperPast), PeriodType.days()); if (presentPeriod.getDays() == pastPeriod.getDays()) { // realign presentPeriod = new Period(new LocalDate(lowerPresent), (new LocalDate(upperPresent)).plusDays(1), PeriodType.months()); pastPeriod = new Period(new LocalDate(lowerPast), (new LocalDate(upperPast)).plusDays(1), PeriodType.months()); if (presentPeriod.getMonths() > pastPeriod.getMonths()) { // some days are missing if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) { // month over month Date newUpperPast = new DateTime(upperPastDT.getYear(), upperPastDT.getMonthOfYear(), upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate(); return new IntervalleObject(lowerPast, newUpperPast); } if (lowerPastDT.getDayOfMonth() != 1) { // previous period Date newLowerPast = new DateTime(lowerPastDT.getYear(), lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate(); return new IntervalleObject(newLowerPast, upperPast); } } else { // either already aligned, of some days should // be removed if (upperPastDT.getDayOfMonth() != upperPastDT.dayOfMonth().getMaximumValue()) { /// month over month if (upperPastDT.getMonthOfYear() == 1) { Date newUpperPast = new DateTime(upperPastDT.getYear() - 1, 12, 31, 23, 59) .toDate(); return new IntervalleObject(lowerPast, newUpperPast); } else { upperPastDT = upperPastDT.minusMonths(1); Date newUpperPast = new DateTime(upperPastDT.getYear(), upperPastDT.getMonthOfYear(), upperPastDT.dayOfMonth().getMaximumValue(), 23, 59).toDate(); return new IntervalleObject(lowerPast, newUpperPast); } } if (lowerPastDT.getDayOfMonth() != 1) { // previous period if (lowerPastDT.getMonthOfYear() == 12) { Date newLowerPast = new DateTime(lowerPastDT.getYear() + 1, 1, 1, 0, 0) .toDate(); return new IntervalleObject(newLowerPast, upperPast); } else { lowerPastDT = lowerPastDT.plusMonths(1); Date newLowerPast = new DateTime(lowerPastDT.getYear(), lowerPastDT.getMonthOfYear(), 1, 0, 0).toDate(); return new IntervalleObject(newLowerPast, upperPast); } } } } } } } } return pastInterval; }
From source file:graph.inference.module.LaterThanWorker.java
License:Open Source License
private Interval parseDate(DAGNode date, DateTime now) { String dateStr = date.toString(); if (dateStr.equals("Now") || dateStr.equals("Now-Generally")) return new Interval(now.getMillis(), now.getMillis() + 1); if (dateStr.equals("Today-Indexical")) return new Interval(now.dayOfYear().roundFloorCopy(), now.dayOfYear().roundCeilingCopy()); if (dateStr.equals("Tomorrow-Indexical")) { return new Interval(now.plusDays(1).dayOfYear().roundFloorCopy(), now.plusDays(1).dayOfYear().roundCeilingCopy()); }/*from w w w . j a v a 2s . c o m*/ if (dateStr.equals("Yesterday-Indexical")) { return new Interval(now.minusDays(1).dayOfYear().roundFloorCopy(), now.minusDays(1).dayOfYear().roundCeilingCopy()); } if (dateStr.equals("TheYear-Indexical")) { return new Interval(now.year().roundFloorCopy(), now.year().roundCeilingCopy()); } // Parse the date from the DAGNode String parsePattern = null; for (int i = DATE_PARSE_INTERVALS.length - 1; i >= 0; i--) { StringBuilder newPattern = new StringBuilder("(" + DATE_PARSE_INTERVALS[i]); if (parsePattern != null) newPattern.append(" " + parsePattern); newPattern.append(")"); parsePattern = newPattern.toString(); DateTimeFormatter dtf = DateTimeFormat.forPattern(parsePattern); try { DateTime dateTime = dtf.parseDateTime(dateStr); if (dateTime != null) { switch (i) { case 0: return new Interval(dateTime.getMillis(), dateTime.plusSeconds(1).minusMillis(1).getMillis()); case 1: return new Interval(dateTime.getMillis(), dateTime.plusMinutes(1).minusMillis(1).getMillis()); case 2: return new Interval(dateTime.getMillis(), dateTime.plusHours(1).minusMillis(1).getMillis()); case 3: return new Interval(dateTime.getMillis(), dateTime.plusDays(1).minusMillis(1).getMillis()); case 4: return new Interval(dateTime.getMillis(), dateTime.plusMonths(1).minusMillis(1).getMillis()); case 5: return new Interval(dateTime.getMillis(), dateTime.plusYears(1).minusMillis(1).getMillis()); } } } catch (Exception e) { } } return null; }
From source file:io.druid.query.expression.TimestampExtractExprMacro.java
License:Apache License
@Override public Expr apply(final List<Expr> args) { if (args.size() < 2 || args.size() > 3) { throw new IAE("Function[%s] must have 2 to 3 arguments", name()); }/*from w w w . j a v a 2 s . c o m*/ if (!args.get(1).isLiteral() || args.get(1).getLiteralValue() == null) { throw new IAE("Function[%s] unit arg must be literal", name()); } if (args.size() > 2 && !args.get(2).isLiteral()) { throw new IAE("Function[%s] timezone arg must be literal", name()); } final Expr arg = args.get(0); final Unit unit = Unit.valueOf(((String) args.get(1).getLiteralValue()).toUpperCase()); final DateTimeZone timeZone; if (args.size() > 2) { timeZone = ExprUtils.toTimeZone(args.get(2)); } else { timeZone = DateTimeZone.UTC; } final ISOChronology chronology = ISOChronology.getInstance(timeZone); class TimestampExtractExpr implements Expr { @Nonnull @Override public ExprEval eval(final ObjectBinding bindings) { final DateTime dateTime = new DateTime(arg.eval(bindings).asLong()).withChronology(chronology); switch (unit) { case EPOCH: return ExprEval.of(dateTime.getMillis()); case SECOND: return ExprEval.of(dateTime.secondOfMinute().get()); case MINUTE: return ExprEval.of(dateTime.minuteOfHour().get()); case HOUR: return ExprEval.of(dateTime.hourOfDay().get()); case DAY: return ExprEval.of(dateTime.dayOfMonth().get()); case DOW: return ExprEval.of(dateTime.dayOfWeek().get()); case DOY: return ExprEval.of(dateTime.dayOfYear().get()); case WEEK: return ExprEval.of(dateTime.weekOfWeekyear().get()); case MONTH: return ExprEval.of(dateTime.monthOfYear().get()); case QUARTER: return ExprEval.of((dateTime.monthOfYear().get() - 1) / 3 + 1); case YEAR: return ExprEval.of(dateTime.year().get()); default: throw new ISE("Unhandled unit[%s]", unit); } } @Override public void visit(final Visitor visitor) { arg.visit(visitor); visitor.visit(this); } } return new TimestampExtractExpr(); }
From source file:net.lshift.diffa.config.DailyPeriodUnit.java
License:Apache License
@Override protected DateTime floor(DateTime instant) { return instant.dayOfYear().roundFloorCopy(); }
From source file:net.lshift.diffa.config.DailyPeriodUnit.java
License:Apache License
@Override protected DateTime ceiling(DateTime instant) { return instant.dayOfYear().roundCeilingCopy(); }
From source file:net.schweerelos.timeline.model.Timeline.java
License:Open Source License
public void setInterval(DateTime start, DateTime end) { DateTimeZone defaultTimeZone = DateTimeZone.getDefault(); // make sure we don't start before the start of the intervals (in years) DateTime firstStart = allIntervals.getFirstStart(); firstStart = firstStart.withDayOfYear(firstStart.dayOfYear().getMinimumValue()); if (start != null && start.toDateTime(defaultTimeZone).isBefore(firstStart)) { this.start = firstStart; } else {//w w w . j a v a2s . c o m this.start = start; } // make sure we don't end after the end of the intervals (in years) DateTime lastEnd = allIntervals.getLastEnd(); lastEnd = lastEnd.withDayOfYear(lastEnd.dayOfYear().getMaximumValue()); if (end != null && end.toDateTime(defaultTimeZone).isAfter(lastEnd)) { this.end = lastEnd; } else { this.end = end; } recalculate(); }
From source file:net.sf.jasperreports.functions.standard.DateTimeFunctions.java
License:Open Source License
/** * Returns the number of days in a year. *//*from w w w . ja v a 2s . com*/ @Function("DAYSINYEAR") @FunctionParameters({ @FunctionParameter("dateObj") }) public Integer DAYSINYEAR(Object dateObj) { Date date = convertDateObject(dateObj); if (date == null) { logCannotConvertToDate(); return null; } else { DateTime dt = new DateTime(date); return dt.dayOfYear().getMaximumValue(); } }