Example usage for org.joda.time DateTime getDayOfMonth

List of usage examples for org.joda.time DateTime getDayOfMonth

Introduction

In this page you can find the example usage for org.joda.time DateTime getDayOfMonth.

Prototype

public int getDayOfMonth() 

Source Link

Document

Get the day of month field value.

Usage

From source file:com.aliakseipilko.flightdutytracker.view.adapter.DayBarChartAdapter.java

License:Open Source License

private void processFlightHours() {
    List<BarEntry> barFlightEntries = new ArrayList<>();

    float xValueCount = 0f;
    int maxDaysInMonth = 0;

    long startDay = new DateTime(flights.minDate("startFlightTime")).getDayOfMonth();

    long currentYear = 0;
    long currentMonth = 0;
    long currentDay = 0;
    boolean isMonthWrapping = false;

    for (Flight flight : flights) {

        DateTime startDateTime = new DateTime(flight.getStartFlightTime());
        DateTime endDateTime = new DateTime(flight.getEndFlightTime());

        float decHoursDiff = (Seconds.secondsBetween(startDateTime, endDateTime).getSeconds() / 60f) / 60f;

        //Dont display stats for today as they will be malformed
        if (startDateTime.getDayOfYear() == DateTime.now().getDayOfYear()
                && startDateTime.getYear() == DateTime.now().getYear()) {
            continue;
        }/*from   www .j a v a2  s.  c  o  m*/

        if (currentYear == 0 && currentMonth == 0 && currentDay == 0) {
            currentYear = startDateTime.getYear();
            currentMonth = startDateTime.getMonthOfYear();
            currentDay = startDateTime.getDayOfMonth();

            maxDaysInMonth = determineDaysInMonth((int) currentMonth, (int) currentYear);

            //Add new entry using day of month as x value
            BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
            barFlightEntries.add(newEntry);
            xValueCount++;
        } else {
            //Check if month is wrapping
            if (currentDay + 1 >= maxDaysInMonth) {
                isMonthWrapping = true;
            } else {
                //Check if days are adjacent
                //Add skipped days onto xValueCount to display blank spaces in graph
                if (currentDay + 1 != startDateTime.getDayOfMonth()) {
                    xValueCount = xValueCount + (startDateTime.getDayOfMonth() - currentDay) - 1;
                }
            }
            //Check if the date is the same as the previous flight
            // All flights are provided in an ordered list by the repo
            if (currentDay == startDateTime.getDayOfMonth() && currentMonth == startDateTime.getMonthOfYear()
                    && currentYear == startDateTime.getYear()) {
                //Get last entry in list
                BarEntry lastEntry = barFlightEntries.get(barFlightEntries.size() - 1);
                //Add the additional hours in that day on, X value (the date) does not change
                lastEntry = new BarEntry(lastEntry.getX(), lastEntry.getY() + decHoursDiff);
                //Remove the last entry and add the modified entry instead of it
                barFlightEntries.remove(barFlightEntries.size() - 1);
                barFlightEntries.add(lastEntry);
            } else {
                //Check if days of month wrap around
                if (startDateTime.getMonthOfYear() != currentMonth) {
                    isMonthWrapping = true;
                }

                //New day
                //Update these for the next iteration
                currentYear = startDateTime.getYear();
                currentMonth = startDateTime.getMonthOfYear();
                currentDay = startDateTime.getDayOfMonth();

                //Add new entry using day of month as x value
                BarEntry newEntry = new BarEntry(xValueCount, decHoursDiff);
                barFlightEntries.add(newEntry);
                xValueCount++;
            }
        }
    }

    IAxisValueFormatter xAxisValueFormatter = new DayAxisValueFormatter((int) (startDay - 1), maxDaysInMonth);
    IAxisValueFormatter yAxisValueFormatter = new DefaultAxisValueFormatter(2);
    BarDataSet dataSet = new BarDataSet(barFlightEntries, "Flight Hours");
    dataSet.setValueTextSize(16f);
    BarData barFlightData = new BarData(dataSet);
    barFlightData.setBarWidth(0.9f);
    barFlightData.setHighlightEnabled(false);

    view.setupFlightBarChart(barFlightData, xAxisValueFormatter, yAxisValueFormatter);
}

From source file:com.almende.eve.agent.MeetingAgent.java

License:Apache License

/**
 * Merge the busy intervals of all attendees, and the preferred intervals
 *//*from w ww. j av  a  2 s  .  com*/
private void mergeTimeConstraints() {
    final ArrayList<Interval> infeasibleIntervals = new ArrayList<Interval>();
    final ArrayList<Weight> preferredIntervals = new ArrayList<Weight>();

    final Activity activity = getActivity();
    if (activity != null) {
        // read and merge the stored busy intervals of all attendees
        for (final Attendee attendee : activity.withConstraints().withAttendees()) {
            final String agent = attendee.getAgent();
            if (attendee.getResponseStatus() != RESPONSE_STATUS.declined) {
                if (new Boolean(true).equals(attendee.getOptional())) {
                    // This attendee is optional.
                    // Add its busy intervals to the soft constraints
                    final List<Interval> attendeeBusy = getAgentBusy(agent);
                    if (attendeeBusy != null) {
                        for (final Interval i : attendeeBusy) {
                            final Weight wi = new Weight(i.getStart(), i.getEnd(),
                                    WEIGHT_BUSY_OPTIONAL_ATTENDEE);

                            preferredIntervals.add(wi);
                        }
                    }
                } else {
                    // this attendee is required.
                    // Add its busy intervals to the hard constraints
                    final List<Interval> attendeeBusy = getAgentBusy(agent);
                    if (attendeeBusy != null) {
                        infeasibleIntervals.addAll(attendeeBusy);
                    }
                }
            }
            // else This attendee declined. Ignore this attendees busy
            // interval
        }

        // read the time preferences and add them to the soft constraints
        final List<Preference> preferences = activity.withConstraints().withTime().withPreferences();
        for (final Preference p : preferences) {
            if (p != null) {
                final Weight wi = new Weight(new DateTime(p.getStart()), new DateTime(p.getEnd()),
                        p.getWeight());

                preferredIntervals.add(wi);
            }
        }
    }

    // add office hours profile to the soft constraints
    // TODO: don't include (hardcoded) office hours here, should be handled
    // by a PersonalAgent
    final DateTime timeMin = DateTime.now();
    final DateTime timeMax = timeMin.plusDays(LOOK_AHEAD_DAYS);
    final List<Interval> officeHours = IntervalsUtil.getOfficeHours(timeMin, timeMax);
    for (final Interval i : officeHours) {
        final Weight wi = new Weight(i, WEIGHT_OFFICE_HOURS);
        preferredIntervals.add(wi);
    }

    // add delay penalties to the soft constraints
    final DateTime now = DateTime.now();
    final MutableDateTime d = new MutableDateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0,
            0, 0, 0);
    for (int i = 0; i <= LOOK_AHEAD_DAYS; i++) {
        final DateTime start = d.toDateTime();
        final DateTime end = start.plusDays(1);
        final Weight wi = new Weight(start, end, WEIGHT_DELAY_PER_DAY * i);
        preferredIntervals.add(wi);
        d.addDays(1);
    }

    // order and store the aggregated lists with intervals
    IntervalsUtil.order(infeasibleIntervals);
    getState().put("infeasible", infeasibleIntervals);
    WeightsUtil.order(preferredIntervals);
    getState().put("preferred", preferredIntervals);
}

From source file:com.anrisoftware.sscontrol.dns.service.DnsServiceImpl.java

License:Open Source License

private int generateSerial(int serial) {
    DateTime date = new DateTime();
    String string = format("%d%d%d%02d", date.getYear(), date.getMonthOfYear(), date.getDayOfMonth(), serial);
    return Integer.parseInt(string);
}

From source file:com.barchart.feed.ddf.message.enums.DDF_TradeDay.java

License:BSD License

/**
 * From millis utc.//from  w ww .  ja  v  a  2 s .co m
 *
 * @param millisUTC the millis utc
 * @return the dD f_ trade day
 */
public static final DDF_TradeDay fromMillisUTC(final long millisUTC) {
    final DateTime dateTime = new DateTime(millisUTC, DateTimeZone.UTC);
    return fromDay(dateTime.getDayOfMonth());
}

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
 * /*from w  w w .  j av a  2 s .c o  m*/
 * 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 . j ava 2  s .c  o  m*/
    encodeInt(date.getDayOfMonth(), buffer, 2);
    buffer.put(SLASH);
    encodeInt(date.getYear(), buffer, 4);
    buffer.put(COMMA);

}

From source file:com.barchart.feed.ddf.util.HelperDDF.java

License:BSD License

/**
 * from millisUTC into ddf "20100616124807".
 * /* w  ww  .j  a va 2 s.co  m*/
 * @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.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 {//ww w. j  a  va  2  s.com
        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  .  jav  a 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;
}