Example usage for org.joda.time Weeks weeksIn

List of usage examples for org.joda.time Weeks weeksIn

Introduction

In this page you can find the example usage for org.joda.time Weeks weeksIn.

Prototype

public static Weeks weeksIn(ReadableInterval interval) 

Source Link

Document

Creates a Weeks representing the number of whole weeks in the specified interval.

Usage

From source file:com.cisco.dvbu.ps.utils.date.DateDiffTimestamp.java

License:Open Source License

/**
 * Called to invoke the stored procedure.  Will only be called a
 * single time per instance.  Can throw CustomProcedureException or
 * SQLException if there is an error during invoke.
 *//*w w  w .j a  v  a 2 s.  c  om*/
public void invoke(Object[] inputValues) throws CustomProcedureException, SQLException {
    Timestamp startTimestamp = null;
    Timestamp endTimestamp = null;
    Calendar startCal = null;
    Calendar endCal = null;
    DateTime startDateTime = null;
    DateTime endDateTime = null;
    String datePart = null;
    long dateLength = 0;

    try {
        result = null;
        if (inputValues[0] == null) {
            result = new Long(dateLength);
            return;
        }

        if (inputValues[1] == null) {
            result = new Long(dateLength);
            return;
        }

        if (inputValues[2] == null) {
            result = new Long(dateLength);
            return;
        }

        datePart = (String) inputValues[0];
        startTimestamp = (Timestamp) inputValues[1];
        // long startMilliseconds = startTimestamp.getTime() +
        // (startTimestamp.getNanos() / 1000000);
        long startMilliseconds = startTimestamp.getTime()
                + (startTimestamp.getNanos() % 1000000L >= 500000L ? 1 : 0);
        startCal = Calendar.getInstance();
        startCal.setTimeInMillis(startMilliseconds);

        endTimestamp = (Timestamp) inputValues[2];
        // long endMilliseconds = endTimestamp.getTime() +
        // (endTimestamp.getNanos() / 1000000);
        long endMilliseconds = endTimestamp.getTime() + (endTimestamp.getNanos() % 1000000L >= 500000L ? 1 : 0);

        endCal = Calendar.getInstance();
        endCal.setTimeInMillis(endMilliseconds);

        startDateTime = new DateTime(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH) + 1,
                startCal.get(Calendar.DAY_OF_MONTH), startCal.get(Calendar.HOUR_OF_DAY),
                startCal.get(Calendar.MINUTE), startCal.get(Calendar.SECOND),
                startCal.get(Calendar.MILLISECOND));
        endDateTime = new DateTime(endCal.get(Calendar.YEAR), endCal.get(Calendar.MONTH) + 1,
                endCal.get(Calendar.DAY_OF_MONTH), endCal.get(Calendar.HOUR_OF_DAY),
                endCal.get(Calendar.MINUTE), endCal.get(Calendar.SECOND), endCal.get(Calendar.MILLISECOND));

        Interval interval = new Interval(startDateTime, endDateTime);

        if (datePart.equalsIgnoreCase("second") || datePart.equalsIgnoreCase("ss")) {
            Seconds seconds = Seconds.secondsIn(interval);
            dateLength = seconds.getSeconds();
        } else if (datePart.equalsIgnoreCase("minute") || datePart.equalsIgnoreCase("mi")) {
            Minutes minutes = Minutes.minutesIn(interval);
            dateLength = minutes.getMinutes();
        } else if (datePart.equalsIgnoreCase("hour") || datePart.equalsIgnoreCase("hh")) {
            Hours hours = Hours.hoursIn(interval);
            dateLength = hours.getHours();
        } else if (datePart.equalsIgnoreCase("day") || datePart.equalsIgnoreCase("dd")) {
            Days days = Days.daysIn(interval);
            dateLength = days.getDays();
        } else if (datePart.equalsIgnoreCase("week") || datePart.equalsIgnoreCase("wk")) {
            Weeks weeks = Weeks.weeksIn(interval);
            dateLength = weeks.getWeeks();
        } else if (datePart.equalsIgnoreCase("month") || datePart.equalsIgnoreCase("mm")) {
            Months months = Months.monthsIn(interval);
            dateLength = months.getMonths();
        } else if (datePart.equalsIgnoreCase("year") || datePart.equalsIgnoreCase("yy")) {
            Years years = Years.yearsIn(interval);
            dateLength = years.getYears();
        } else if (datePart.equalsIgnoreCase("millisecond") || datePart.equalsIgnoreCase("ms")) {
            dateLength = (endTimestamp.getTime() - startTimestamp.getTime()); // millis
        } else if (datePart.equalsIgnoreCase("microsecond") || datePart.equalsIgnoreCase("mcs")) {
            dateLength = ((endTimestamp.getTime() - startTimestamp.getTime()) / 1000) // seconds
                    * 1000000L // micros
                    + (endTimestamp.getNanos() - startTimestamp.getNanos()) / 1000; // nanos/1000
        } else if (datePart.equalsIgnoreCase("nanosecond") || datePart.equalsIgnoreCase("ns")) {
            dateLength = ((endTimestamp.getTime() - startTimestamp.getTime()) / 1000) // seconds
                    * 1000000000L // nanos
                    + (endTimestamp.getNanos() - startTimestamp.getNanos()); // nanos
        } else {
            throw new IllegalArgumentException(datePart);
        }

        result = new Long(dateLength);

    } catch (Throwable t) {
        throw new CustomProcedureException(t);
    }
}

From source file:net.schweerelos.timeline.model.Timeline.java

License:Open Source License

private void recalculate() {
    if (start == null || end == null) {
        logger.warn("recalculating aborted, start and/or end is null");
        numSlices = 0;/*from www . j a  v  a 2  s  . c o m*/
        return;
    }
    Interval interval = new Interval(start, end);

    if (Years.yearsIn(interval).isGreaterThan(Years.ZERO)) {
        // make it start at the start of the current increment mode
        start = start.withDayOfYear(start.dayOfYear().getMinimumValue());
        end = end.withDayOfYear(end.dayOfYear().getMaximumValue());
        interval = new Interval(start, end);

        // figure out number of slices
        numSlices = Years.yearsIn(interval).getYears();
        if (start.plusYears(numSlices).isBefore(end)) {
            numSlices += 1;
        }

        // update label extractor
        sliceLabelExtractor = new SliceLabelExtractor() {
            @Override
            public String extractLabel(DateTime from) {
                return from.year().getAsShortText();
            }
        };

        // update increment
        increment = Years.ONE.toPeriod();
        incrementMode = Mode.Years;
    } else if (Months.monthsIn(interval).isGreaterThan(Months.ZERO)) {
        // make it start at the start of the current increment mode
        start = start.withDayOfMonth(start.dayOfMonth().getMinimumValue());
        end = end.withDayOfMonth(end.dayOfMonth().getMaximumValue());
        interval = new Interval(start, end);

        numSlices = Months.monthsIn(interval).getMonths();
        if (start.plusMonths(numSlices).isBefore(end)) {
            numSlices += 1;
        }

        sliceLabelExtractor = new SliceLabelExtractor() {
            @Override
            public String extractLabel(DateTime from) {
                return from.monthOfYear().getAsShortText();
            }
        };

        increment = Months.ONE.toPeriod();
        incrementMode = Mode.Months;
    } else if (Weeks.weeksIn(interval).isGreaterThan(Weeks.ZERO)) {
        start = start.withDayOfWeek(start.dayOfWeek().getMinimumValue());
        end = end.withDayOfWeek(end.dayOfWeek().getMaximumValue());
        interval = new Interval(start, end);

        numSlices = Weeks.weeksIn(interval).getWeeks();
        if (start.plusWeeks(numSlices).isBefore(end)) {
            numSlices += 1;
        }

        sliceLabelExtractor = new SliceLabelExtractor() {
            @Override
            public String extractLabel(DateTime from) {
                return "W" + from.weekOfWeekyear().getAsShortText();
            }
        };

        increment = Weeks.ONE.toPeriod();
        incrementMode = Mode.Weeks;
    } else {
        numSlices = Days.daysIn(interval).getDays();
        if (start.plusDays(numSlices).isBefore(end)) {
            numSlices += 1;
        }
        if (numSlices == 0) {
            // force at least one day to be drawn
            numSlices = 1;
        }

        sliceLabelExtractor = new SliceLabelExtractor() {
            @Override
            public String extractLabel(DateTime from) {
                return from.dayOfMonth().getAsShortText();
            }
        };

        increment = Days.ONE.toPeriod();
        incrementMode = Mode.Days;
    }

    // reset time of day too
    start = start.withMillisOfDay(start.millisOfDay().getMinimumValue());
    end = end.withMillisOfDay(end.millisOfDay().getMaximumValue());

    // recalculate which intervals are within range
    intervalsWithinRange.clear();
    intervalsWithinRange.addAll(calculateIntervalsWithinRange(start, end));

    // notify listeners
    changeSupport.firePropertyChange(INTERVAL_PROPERTY_KEY, interval, new Interval(start, end));
}