List of usage examples for org.joda.time DateTime getMonthOfYear
public int getMonthOfYear()
From source file:com.barchart.feed.ddf.message.enums.DDF_TradeDay.java
License:BSD License
/** * recover full trade date from DDF day code and todays date * /*www. ja va 2s . c om*/ * expressed in UTC zone * * year, month, day : should be treated as local market trade date. * * @param tradeDay the trade day * @param todayDate the today date * @return the time value */ public static TimeValue tradeDateFrom(final DDF_TradeDay tradeDay, final DateTime todayDate) { // trading day of month reported by the feed final int tradingDayNum = tradeDay.day; // current day of month final int currentDayNum = todayDate.getDayOfMonth(); // positive for same month if trading date is in the future // unless day enum is not a day in the month ??? final int difference = tradingDayNum - currentDayNum; final boolean isSmall = Math.abs(difference) <= HOLIDAY_THESHOLD; final boolean isLarge = !isSmall; // final boolean isSameMonthSameDay = (difference == 0); final boolean isSameMonthPastDay = difference < 0 & isSmall; final boolean isSameMonthNextDay = difference > 0 & isSmall; final boolean isPastMonthPastDay = difference > 0 & isLarge; final boolean isNextMonthNextDay = difference < 0 & isLarge; // DateTime generated; try { if (isSameMonthSameDay) { generated = todayDate; } else if (isSameMonthPastDay) { generated = todayDate.withDayOfMonth(tradingDayNum); } else if (isSameMonthNextDay) { generated = todayDate.withDayOfMonth(tradingDayNum); } else if (isPastMonthPastDay) { generated = todayDate.minusMonths(1).withDayOfMonth(tradingDayNum); } else if (isNextMonthNextDay) { generated = todayDate.plusMonths(1).withDayOfMonth(tradingDayNum); } else { logger.error("should not happen"); generated = todayDate; } } catch (final Exception e) { generated = todayDate; } final DateTime result = new DateTime(// generated.getYear(), // generated.getMonthOfYear(), // generated.getDayOfMonth(), // 0, 0, 0, 0, ZONE_UTC); final long millisUTC = result.getMillis(); return ValueBuilder.newTime(millisUTC); }
From source file:com.barchart.feed.ddf.message.provider.BaseEOD.java
License:BSD License
protected final void encodeDay(final ByteBuffer buffer) { final DateTime date = new DateTime(getTradeDay().tradeDate().asMillisUTC(), DateTimeZone.UTC); encodeInt(date.getMonthOfYear(), buffer, 2); buffer.put(SLASH);/*from w w w.ja v a2 s. c om*/ encodeInt(date.getDayOfMonth(), buffer, 2); buffer.put(SLASH); encodeInt(date.getYear(), buffer, 4); buffer.put(COMMA); }
From source file:com.barchart.feed.ddf.symbol.enums.DDF_ExpireMonth.java
License:BSD License
/** * From date time./*from w w w .ja v a 2 s.co m*/ * * @param dateTime the date time * @return the dD f_ expire month */ public static final DDF_ExpireMonth fromDateTime(final DateTime dateTime) { if (dateTime == null) { return UNKNOWN; } // DateTime 'default' calendar is the ISO8601 // JAN==1 ... DEC==12 final int month = dateTime.getMonthOfYear(); switch (month) { case 1: return JAN; case 2: return FEB; case 3: return MAR; case 4: return APR; case 5: return MAY; case 6: return JUN; case 7: return JUL; case 8: return AUG; case 9: return SEP; case 10: return OCT; case 11: return NOV; case 12: return DEC; default: return UNKNOWN; } }
From source file:com.barchart.feed.ddf.util.HelperDDF.java
License:BSD License
/** * from millisUTC into ddf "20100616124807". * //from w ww. j a va 2 s . c om * @param millisUTC * the millis utc * @param zone * the zone * @return the long */ public static final long timeEncode(final long millisUTC, final DateTimeZone zone) { final DateTime dateTime = new DateTime(millisUTC, zone); long value = 0; final int year = dateTime.getYearOfEra(); value += year; value *= 100; final int month = dateTime.getMonthOfYear(); value += month; value *= 100; final int day = dateTime.getDayOfMonth(); value += day; value *= 100; final int hour = dateTime.getHourOfDay(); value += hour; value *= 100; final int minute = dateTime.getMinuteOfHour(); value += minute; value *= 100; final int second = dateTime.getSecondOfMinute(); value += second; return value; }
From source file:com.barchart.feed.test.replay.DDFLogDeframer.java
License:BSD License
static final void encodeTimeStamp(final String timestamp, final ByteBuffer buffer) { DateTime dateTime = timeParser.parseDateTime(timestamp); // base fields buffer.put(DDF_CENTURY); // century buffer.put(encodeTimeStampByte(dateTime.getYearOfCentury())); // year buffer.put(encodeTimeStampByte(dateTime.getMonthOfYear())); // month buffer.put(encodeTimeStampByte(dateTime.getDayOfMonth())); // day buffer.put(encodeTimeStampByte(dateTime.getHourOfDay())); // hours buffer.put(encodeTimeStampByte(dateTime.getMinuteOfHour())); // minutes buffer.put(encodeTimeStampByte(dateTime.getSecondOfMinute())); // seconds // milliseconds final int millisOfSecond = dateTime.getMillisOfSecond(); buffer.put((byte) (millisOfSecond & 0xFF)); // low byte buffer.put((byte) ((millisOfSecond >>> 8) & 0xFF)); // high byte }
From source file:com.boha.golfkids.util.NewGolfGroupUtil.java
private static Date getDateOfBirth(int age) { DateTime dt = new DateTime(); dt = dt.minusYears(age);//from w ww .j av a 2s .co m Random rand = new Random(System.currentTimeMillis()); int mth = rand.nextInt(dt.getMonthOfYear()); int day = rand.nextInt(28); if (mth == 0) { mth = 1; } if (day == 0) { day = 1; } DateTime x = new DateTime(dt.getYear(), mth, day, 0, 0); return new Date(x.getMillis()); }
From source file:com.bskyb.cg.environments.message.AuditdMessageFormat.java
License:Apache License
@Override public Message parse(byte[] inMessage) throws ParseException { Message message = new LogMessage(); String textMessage = new String(inMessage); StringTokenizer st = new StringTokenizer(textMessage, delimiter); String msgType = (String) st.nextElement(); String timeStamp = (String) st.nextElement(); DateTime longDate = null; try {//from w w w .j a v a 2 s . c om longDate = parseDate(timeStamp); } catch (Exception e) { e.printStackTrace(); } if (longDate == null) { return message; } String year = new Integer(longDate.getYear()).toString(); String month = String.format("%02d", new Integer(longDate.getMonthOfYear())); String day = String.format("%02d", new Integer(longDate.getDayOfMonth())); String hostName = (String) st.nextElement(); long epochTime = longDate.getMillis(); UUID uuid = TimeUUIDUtils.getTimeUUID(epochTime); String filename = year + "-" + month + "-" + day + "_" + hostName + "_" + msgType + "_" + uuid.toString() + "_" + new Long(epochTime).toString(); String messageText = (String) st.nextElement(); message.setKey(filename); message.setMessage(messageText.getBytes()); return message; }
From source file:com.cfelde.cron4joda.SchedulingPattern.java
License:Open Source License
/** * This methods returns true if the given timestamp (expressed as a UNIX-era * millis value) matches the pattern, according to the given time zone. * * @param timezone A time zone./*from w w w . j a va 2 s . c o m*/ * @param millis The timestamp, as a UNIX-era millis value. * @return true if the given timestamp matches the pattern. */ public boolean match(DateTime dt) { int minute = dt.getMinuteOfHour(); int hour = dt.getHourOfDay(); int dayOfMonth = dt.getDayOfMonth(); int month = dt.getMonthOfYear(); //gc.get(Calendar.MONTH) + 1; int dayOfWeek = dt.getDayOfWeek(); //gc.get(Calendar.DAY_OF_WEEK) - 1; for (int i = 0; i < matcherSize; i++) { ValueMatcher minuteMatcher = (ValueMatcher) minuteMatchers.get(i); ValueMatcher hourMatcher = (ValueMatcher) hourMatchers.get(i); ValueMatcher dayOfMonthMatcher = (ValueMatcher) dayOfMonthMatchers.get(i); ValueMatcher monthMatcher = (ValueMatcher) monthMatchers.get(i); ValueMatcher dayOfWeekMatcher = (ValueMatcher) dayOfWeekMatchers.get(i); boolean eval = minuteMatcher.match(minute) && hourMatcher.match(hour) && ((dayOfMonthMatcher instanceof DayOfMonthValueMatcher) ? ((DayOfMonthValueMatcher) dayOfMonthMatcher).match(dayOfMonth, month, dt.year().getField().isLeap(dt.getMillis())) /*gc.isLeapYear(year))*/ : dayOfMonthMatcher.match(dayOfMonth)) && monthMatcher.match(month) && dayOfWeekMatcher.match(dayOfWeek); if (eval) { return true; } } return false; }
From source file:com.clevercloud.bianca.lib.date.DateModule.java
License:Open Source License
/** * Returns the parsed date./*from ww w . j a va 2 s . c om*/ */ public Value strptime(Env env, String date, String format) { ArrayValueImpl array = new ArrayValueImpl(); DateTimeFormatterBuilder fb = new DateTimeFormatterBuilder(); int length = format.length(); for (int i = 0; i < length; i++) { char ch = format.charAt(i); if (ch != '%') { fb.appendLiteral(ch); continue; } switch (format.charAt(++i)) { case 'a': fb.appendDayOfWeekShortText(); break; case 'A': fb.appendDayOfWeekText(); break; case 'h': case 'b': fb.appendMonthOfYearShortText(); ; break; case 'B': fb.appendMonthOfYearText(); break; // TODO: case 'c' case 'C': fb.appendCenturyOfEra(2, 2); break; case 'd': fb.appendDayOfMonth(2); break; case 'D': fb.appendMonthOfYear(2); fb.appendLiteral('/'); fb.appendDayOfMonth(2); fb.appendLiteral('/'); fb.appendYear(2, 2); break; // TODO: case 'e' case 'F': fb.appendYear(4, 4); fb.appendLiteral('-'); fb.appendMonthOfYear(2); fb.appendLiteral('-'); fb.appendDayOfMonth(2); break; // TODO: case 'g' // TODO: case 'G' case 'H': fb.appendHourOfDay(2); break; case 'I': fb.appendHourOfHalfday(2); break; case 'j': fb.appendDayOfYear(3); break; // TODO: case 'l' case 'm': fb.appendMonthOfYear(2); break; case 'M': fb.appendMinuteOfHour(2); break; case 'n': fb.appendLiteral("\n"); break; case 'p': case 'P': fb.appendHalfdayOfDayText(); break; case 'r': fb.appendHourOfHalfday(2); fb.appendLiteral(':'); fb.appendMinuteOfHour(2); fb.appendLiteral(':'); fb.appendSecondOfMinute(2); fb.appendLiteral(' '); fb.appendHalfdayOfDayText(); break; case 'R': fb.appendHourOfDay(2); fb.appendLiteral(':'); fb.appendMinuteOfHour(2); break; // TODO: case 's' case 'S': fb.appendSecondOfMinute(2); break; case 't': fb.appendLiteral("\t"); break; case 'T': fb.appendHourOfDay(2); fb.appendLiteral(':'); fb.appendMinuteOfHour(2); fb.appendLiteral(':'); fb.appendSecondOfMinute(2); break; // TODO: case 'u' // TODO: case 'U' // TODO: case 'V' // TODO: case 'w' // TODO: case 'W' // TODO: case 'x' // TODO: case 'X' case 'y': fb.appendYear(2, 2); break; case 'Y': fb.appendYear(4, 4); break; case 'z': fb.appendTimeZoneOffset(null, true, 2, 2); break; case 'Z': fb.appendTimeZoneName(); break; case '%': fb.appendLiteral('%'); break; default: fb.appendLiteral(ch); } } DateTimeFormatter dtf = fb.toFormatter().withLocale(Locale.getDefault()).withOffsetParsed(); org.joda.time.DateTime dt = new org.joda.time.DateTime(); String unparsed = ""; try { dt = dtf.parseDateTime(date); } catch (IllegalArgumentException e) { String delims = "[\"]+"; String[] splits = e.getMessage().split(delims); unparsed = unparsed.concat(splits[3]); } // According to manual strptime(3) if (dt.getCenturyOfEra() == 0) { if (dt.getYear() > 68) { dt = dt.withCenturyOfEra(19); } else { dt = dt.withCenturyOfEra(20); } } array.put("tm_sec", dt.getSecondOfMinute()); array.put("tm_min", dt.getMinuteOfHour()); array.put("tm_hour", dt.getHourOfDay()); array.put("tm_mday", dt.getDayOfMonth()); array.put("tm_mon", dt.getMonthOfYear() - 1); array.put("tm_year", dt.getYearOfCentury() + ((dt.getCenturyOfEra() - 19) * 100)); // Years since 1900 array.put("tm_wday", dt.getDayOfWeek() % 7); array.put("tm_yday", dt.getDayOfYear() - 1); array.put("unparsed", unparsed); return array; }
From source file:com.cloudhopper.commons.util.DateTimeUtil.java
License:Apache License
/** * Null-safe method that returns a new instance of a DateTime object rounded * downwards to the nearest month. Note that the nearest valid month is actually * the first of that given month (1st day of month). The time zone of the returned DateTime * instance will be the same as the argument. Similar to a floor() function * on a float.<br>/* w ww. j av a 2 s .c o m*/ * Examples: * <ul> * <li>null -> null * <li>"2009-06-24 13:24:51.476 -8:00" -> "2009-06-01 00:00:00.000 -8:00" * </ul> * @param value The DateTime value to round downward * @return Null if the argument is null or a new instance of the DateTime * value rounded downwards to the nearest month. */ public static DateTime floorToMonth(DateTime value) { if (value == null) { return null; } return new DateTime(value.getYear(), value.getMonthOfYear(), 1, 0, 0, 0, 0, value.getZone()); }