Example usage for org.joda.time MutableDateTime getDayOfMonth

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

Introduction

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

Prototype

public int getDayOfMonth() 

Source Link

Document

Get the day of month field value.

Usage

From source file:com.alliander.osgp.adapter.protocol.oslp.elster.application.mapping.OslpGetConfigurationResponseToConfigurationConverter.java

License:Open Source License

/**
 * For a given Month of this year, find the date for the weekday {@link day}
 * .//from   w w  w  . j  a va2 s  . c  o m
 */
private int getLastDayOfMonth(final int month, final int day) {
    final DateTime dateTime = DateTime.now();
    MutableDateTime x = dateTime.toMutableDateTime();
    x.set(DateTimeFieldType.monthOfYear(), month);
    x.set(DateTimeFieldType.dayOfMonth(), 31);

    x = this.findLastDayOfOfMonth(day, x);
    return x.getDayOfMonth();
}

From source file:com.alliander.osgp.adapter.protocol.oslp.elster.application.mapping.OslpGetConfigurationResponseToConfigurationConverter.java

License:Open Source License

/**
 * Loop backwards through the days of the month until we find {@link day} of
 * the month. For example the last Sunday of the month March of this year.
 *//*from w w w .  ja va2  s.  c  o  m*/
private MutableDateTime findLastDayOfOfMonth(final int day, final MutableDateTime x) {
    final int yodaTimeDay = day + 1;
    while (true) {
        if (yodaTimeDay == x.getDayOfWeek()) {
            break;
        } else {
            final int dayOfMonth = x.getDayOfMonth() - 1;
            x.set(DateTimeFieldType.dayOfMonth(), dayOfMonth);
        }
    }
    return x;
}

From source file:com.tmathmeyer.sentinel.ui.navigation.MiniMonth.java

License:Open Source License

public MiniMonth(DateTime time, final MiniCalendarHostIface mc, boolean monthOnly) {
    this.setLayout(new GridLayout(7, 7));
    MutableDateTime prevMonth = new MutableDateTime(time);
    prevMonth.setDayOfMonth(1);/*from   www  .jav a 2 s.  c  o  m*/
    prevMonth.addMonths(-1); // What is prevMonth for?
    String[] dayLabel = { "S", "M", "T", "W", "R", "F", "S" };

    MouseListener monthChanger = new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent me) {
        }

        @Override
        public void mouseEntered(MouseEvent me) {
        }

        @Override
        public void mouseExited(MouseEvent me) {
        }

        @Override
        public void mousePressed(MouseEvent me) {
        }

        @Override
        public void mouseReleased(MouseEvent me) {
            DayLabel d = (DayLabel) (me.getSource());
            if (!(d instanceof DescriptiveDayLabel)) {
                mc.display(d.getMonth());
            }
        }
    };

    MutableDateTime referenceDay = new MutableDateTime(time);
    // reset to the first of the month at midnight, then find Sunday
    referenceDay.setDayOfMonth(1);
    referenceDay.setMillisOfDay(0);
    int first = referenceDay.getDayOfWeek();
    referenceDay.addDays(-first);
    boolean flipFlop = false;

    // add day labels
    for (int i = 0; i < 7; i++) {
        DayLabel day = new DescriptiveDayLabel(dayLabel[i], time);
        day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6);
        add(day);
        day.addMouseListener(monthChanger);
    }

    // generate days, 6*7 covers all possible months, so we just loop
    // through and add each day
    for (int i = 0; i < (6 * 7); i++) {
        DayLabel day;
        if (monthOnly || MainPanel.getInstance().getView() == ViewSize.Month) {
            if (referenceDay.getDayOfMonth() == 1)
                flipFlop ^= true; // flops the flip flop flappity flip
        } else if (MainPanel.getInstance().getView() == ViewSize.Day)
            flipFlop = referenceDay.getDayOfYear() == time.getDayOfYear()
                    && referenceDay.getYear() == time.getYear();
        else if (MainPanel.getInstance().getView() == ViewSize.Week) {
            if (Months.getWeekStart(time).getMonthOfYear() == 12
                    && Months.getWeekStart(time).getDayOfMonth() >= 26) // Exception
                // case
                // for
                // weeks
                // between
                // years
                flipFlop = time.getMonthOfYear() == 12 ? i >= 35 : i <= 6;
            else
                flipFlop = referenceDay.getDayOfYear() >= Months.getWeekStart(time).getDayOfYear()
                        && referenceDay.getDayOfYear() <= Months.getWeekStart(time).getDayOfYear() + 6;
        }

        if (flipFlop)
            day = new ActiveDayLabel(referenceDay.toDateTime());
        else
            day = new InactiveDayLabel(referenceDay.toDateTime());

        day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6);
        add(day);
        day.addMouseListener(monthChanger);
        referenceDay.addDays(1); // go to next day
    }
}

From source file:com.tmathmeyer.sentinel.ui.views.year.YearCalendar.java

License:Open Source License

/**
 * gets the drawn year/*from   w  ww .j  ava2 s.c o  m*/
 * 
 * @param start the date time that starts the series.
 * @return the jpanel that contains the colored days and the S/M/T/W/R/F/S
 *         labels
 */
private JPanel getFourMonthGrid(MutableDateTime start) {
    int gridHeight = 19;
    int gridWidth = 7;

    JPanel p = new JPanel();
    p.setLayout(new GridLayout(gridHeight, gridWidth));

    String[] days = { "S", "M", "T", "W", "R", "F", "S" };
    for (int i = 0; i < 7; i++) {
        JLabel header = new JLabel(days[i]);
        header.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Colors.BORDER));
        header.setHorizontalAlignment(SwingConstants.CENTER);
        header.setFont(new Font("DejaVu Sans", Font.ITALIC, 12));
        p.add(header);
    }

    for (int j = 0; j < 18 * 7; j++) {
        Color dayBackground = start.getMonthOfYear() % 2 == 0 ? Colors.TABLE_BACKGROUND
                : Colors.TABLE_GRAY_HEADER;

        Integer eventCount = events.get(start.getDayOfYear());
        eventCount = eventCount == null ? 0 : eventCount;

        YearlyDayHolder day = new YearlyDayHolder(start.toDateTime(), dayBackground);
        MutableDateTime today = new MutableDateTime(DateTime.now());
        today.setMillisOfDay(0);
        MutableDateTime checking = new MutableDateTime(start);
        start.setMillisOfDay(0);
        if (checking.toDateTime().isEqual(today)) {
            day.setBackground(Colors.SELECTED_BACKGROUND);
            day.setForeground(Colors.SELECTED_TEXT);
        }
        JLabel dayLabel = new JLabel(start.getDayOfMonth() + "");
        dayLabel.setHorizontalAlignment(SwingConstants.CENTER);

        day.setLayout(new GridLayout(1, 1));
        day.add(dayLabel);
        day.setBorder(
                BorderFactory.createMatteBorder(0, start.getDayOfWeek() % 7 == 0 ? 1 : 0, 1, 1, Colors.BORDER));

        day.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent me) {
                YearlyDayHolder event = (YearlyDayHolder) (me.getSource());
                MainPanel.getInstance().miniMove(event.getDateTime());
                MainPanel.getInstance().viewDay();
            }

            @Override
            public void mouseEntered(MouseEvent me) {
                // this is just a demo of what it can do
            }

            @Override
            public void mouseExited(MouseEvent me) {
            }

            @Override
            public void mousePressed(MouseEvent me) {
                // TODO: something? maybe nothing? have to decide with
                // team/steakholders
            }

            @Override
            public void mouseReleased(MouseEvent me) {
                YearlyDayHolder event = (YearlyDayHolder) (me.getSource());
                MainPanel.getInstance().miniMove(event.getDateTime());
            }

        });

        p.add(day);
        start.addDays(1);
    }

    int width = 280;
    int height = 570;

    p.setMinimumSize(new Dimension(0, height - 150));
    p.setPreferredSize(new Dimension(width, height));
    p.setMaximumSize(new Dimension(width + 350, height + 150));

    return p;
}

From source file:edu.wpi.cs.wpisuitetng.modules.cal.ui.navigation.MiniMonth.java

License:Open Source License

public MiniMonth(DateTime time, final MiniCalendarHostIface mc, boolean monthOnly) {
    this.setLayout(new GridLayout(7, 7));
    MutableDateTime prevMonth = new MutableDateTime(time);
    prevMonth.setDayOfMonth(1);//from  w  w  w.j av  a2 s.c  o m
    prevMonth.addMonths(-1); // What is prevMonth for?
    String[] dayLabel = { "S", "M", "T", "W", "R", "F", "S" };

    MouseListener monthChanger = new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent me) {
        }

        @Override
        public void mouseEntered(MouseEvent me) {
        }

        @Override
        public void mouseExited(MouseEvent me) {
        }

        @Override
        public void mousePressed(MouseEvent me) {
        }

        @Override
        public void mouseReleased(MouseEvent me) {
            DayLabel d = (DayLabel) (me.getSource());
            if (!(d instanceof DescriptiveDayLabel)) {
                mc.display(d.getMonth());
            }
        }
    };

    MutableDateTime referenceDay = new MutableDateTime(time);
    // reset to the first of the month at midnight, then find Sunday
    referenceDay.setDayOfMonth(1);
    referenceDay.setMillisOfDay(0);
    int first = referenceDay.getDayOfWeek();
    referenceDay.addDays(-first);
    boolean flipFlop = false;

    // add day labels
    for (int i = 0; i < 7; i++) {
        DayLabel day = new DescriptiveDayLabel(dayLabel[i], time);
        day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6);
        add(day);
        day.addMouseListener(monthChanger);
    }

    // generate days, 6*7 covers all possible months, so we just loop
    // through and add each day
    for (int i = 0; i < (6 * 7); i++) {
        DayLabel day;
        if (monthOnly || MainPanel.getInstance().getView() == ViewSize.Month) {
            if (referenceDay.getDayOfMonth() == 1)
                flipFlop ^= true; // flops the flip flop flappity flip
        } else if (MainPanel.getInstance().getView() == ViewSize.Day)
            flipFlop = referenceDay.getDayOfYear() == time.getDayOfYear()
                    && referenceDay.getYear() == time.getYear();
        else if (MainPanel.getInstance().getView() == ViewSize.Week) {
            if (Months.getWeekStart(time).getMonthOfYear() == 12
                    && Months.getWeekStart(time).getDayOfMonth() >= 26) // Exception case for weeks between years
                flipFlop = time.getMonthOfYear() == 12 ? i >= 35 : i <= 6;
            else
                flipFlop = referenceDay.getDayOfYear() >= Months.getWeekStart(time).getDayOfYear()
                        && referenceDay.getDayOfYear() <= Months.getWeekStart(time).getDayOfYear() + 6;
        }

        if (flipFlop)
            day = new ActiveDayLabel(referenceDay.toDateTime());
        else
            day = new InactiveDayLabel(referenceDay.toDateTime());

        day.borderize((i % 7) == 0, i >= 5 * 7, (i % 7) == 6);
        add(day);
        day.addMouseListener(monthChanger);
        referenceDay.addDays(1); // go to next day
    }
}

From source file:edu.wpi.cs.wpisuitetng.modules.cal.ui.views.year.YearCalendar.java

License:Open Source License

/**
 * gets the drawn year//from ww  w . ja va  2  s .c o  m
 * 
 * @param start the date time that starts the series.
 * @return the jpanel that contains the colored days and the S/M/T/W/R/F/S labels
 */
private JPanel getFourMonthGrid(MutableDateTime start) {
    int gridHeight = 19;
    int gridWidth = 7;

    JPanel p = new JPanel();
    p.setLayout(new GridLayout(gridHeight, gridWidth));

    String[] days = { "S", "M", "T", "W", "R", "F", "S" };
    for (int i = 0; i < 7; i++) {
        JLabel header = new JLabel(days[i]);
        header.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Colors.BORDER));
        header.setHorizontalAlignment(SwingConstants.CENTER);
        header.setFont(new Font("DejaVu Sans", Font.ITALIC, 12));
        p.add(header);
    }

    for (int j = 0; j < 18 * 7; j++) {
        Color dayBackground = start.getMonthOfYear() % 2 == 0 ? Colors.TABLE_BACKGROUND
                : Colors.TABLE_GRAY_HEADER;

        Integer eventCount = events.get(start.getDayOfYear());
        eventCount = eventCount == null ? 0 : eventCount;

        YearlyDayHolder day = new YearlyDayHolder(start.toDateTime(), dayBackground);
        MutableDateTime today = new MutableDateTime(DateTime.now());
        today.setMillisOfDay(0);
        MutableDateTime checking = new MutableDateTime(start);
        start.setMillisOfDay(0);
        if (checking.toDateTime().isEqual(today)) {
            day.setBackground(Colors.SELECTED_BACKGROUND);
            day.setForeground(Colors.SELECTED_TEXT);
        }
        JLabel dayLabel = new JLabel(start.getDayOfMonth() + "");
        dayLabel.setHorizontalAlignment(SwingConstants.CENTER);

        day.setLayout(new GridLayout(1, 1));
        day.add(dayLabel);
        day.setBorder(
                BorderFactory.createMatteBorder(0, start.getDayOfWeek() % 7 == 0 ? 1 : 0, 1, 1, Colors.BORDER));

        day.addMouseListener(new MouseListener() {

            @Override
            public void mouseClicked(MouseEvent me) {
                YearlyDayHolder event = (YearlyDayHolder) (me.getSource());
                MainPanel.getInstance().miniMove(event.getDateTime());
                MainPanel.getInstance().viewDay();
            }

            @Override
            public void mouseEntered(MouseEvent me) {
                // this is just a demo of what it can do
            }

            @Override
            public void mouseExited(MouseEvent me) {
            }

            @Override
            public void mousePressed(MouseEvent me) {
                //TODO: something? maybe nothing? have to decide with team/steakholders
            }

            @Override
            public void mouseReleased(MouseEvent me) {
                YearlyDayHolder event = (YearlyDayHolder) (me.getSource());
                MainPanel.getInstance().miniMove(event.getDateTime());
            }

        });

        p.add(day);
        start.addDays(1);
    }

    int width = 280;
    int height = 570;

    p.setMinimumSize(new Dimension(0, height - 150));
    p.setPreferredSize(new Dimension(width, height));
    p.setMaximumSize(new Dimension(width + 350, height + 150));

    return p;
}

From source file:org.talend.components.netsuite.avro.converter.XMLGregorianCalendarToDateTimeConverter.java

License:Open Source License

@Override
public XMLGregorianCalendar convertToDatum(Object timestamp) {
    if (timestamp == null) {
        return null;
    }/*w  w  w.  java 2s  .c  o  m*/

    long timestampMillis;
    if (timestamp instanceof Long) {
        timestampMillis = ((Long) timestamp).longValue();
    } else if (timestamp instanceof Date) {
        timestampMillis = ((Date) timestamp).getTime();
    } else {
        throw new IllegalArgumentException("Unsupported Avro timestamp value: " + timestamp);
    }

    MutableDateTime dateTime = new MutableDateTime();
    dateTime.setMillis(timestampMillis);

    XMLGregorianCalendar xts = datatypeFactory.newXMLGregorianCalendar();
    xts.setYear(dateTime.getYear());
    xts.setMonth(dateTime.getMonthOfYear());
    xts.setDay(dateTime.getDayOfMonth());
    xts.setHour(dateTime.getHourOfDay());
    xts.setMinute(dateTime.getMinuteOfHour());
    xts.setSecond(dateTime.getSecondOfMinute());
    xts.setMillisecond(dateTime.getMillisOfSecond());
    xts.setTimezone(dateTime.getZone().toTimeZone().getOffset(dateTime.getMillis()) / 60000);

    return xts;
}

From source file:org.wso2.analytics.esb.util.TimeRangeUtils.java

License:Open Source License

public static List<TimeRange> getDateTimeRanges(long from, long to) {
    List<TimeRange> ranges = new ArrayList<>(10);
    MutableDateTime fromDate = new MutableDateTime(from);
    fromDate.set(DateTimeFieldType.millisOfSecond(), 0);
    MutableDateTime toDate = new MutableDateTime(to);
    toDate.set(DateTimeFieldType.millisOfSecond(), 0);
    MutableDateTime tempFromTime = fromDate.copy();
    MutableDateTime tempToTime = toDate.copy();

    if (log.isDebugEnabled()) {
        log.debug("Time range: " + formatter.format(fromDate.toDate()) + " -> "
                + formatter.format(toDate.toDate()));
    }//from   w  ww .  j a va  2s.  c o m

    if (toDate.getMillis() - fromDate.getMillis() < DateTimeConstants.MILLIS_PER_MINUTE) {
        ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                new long[] { fromDate.getMillis(), toDate.getMillis() }));
    } else {
        if (tempFromTime.getSecondOfMinute() != 0
                && (toDate.getMillis() - fromDate.getMillis() > DateTimeConstants.MILLIS_PER_MINUTE)) {
            tempFromTime = tempFromTime.minuteOfHour().roundCeiling();
            ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getMinuteOfHour() != 0
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) {
            fromDate = tempFromTime.copy();
            if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE) < 60) {
                tempFromTime = tempFromTime.minuteOfHour().add(
                        (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE);
            } else {
                tempFromTime = tempFromTime.hourOfDay().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.MINUTE.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getHourOfDay() != 0
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) {
            fromDate = tempFromTime.copy();
            if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR) < 24) {
                tempFromTime = tempFromTime.hourOfDay().add(
                        (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR);
            } else {
                tempFromTime = tempFromTime.dayOfMonth().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.HOUR.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getDayOfMonth() != 1
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) {
            fromDate = tempFromTime.copy();
            if ((((toDate.getMillis() - tempFromTime.getMillis())
                    / DateTimeConstants.MILLIS_PER_DAY)) < tempFromTime.dayOfMonth().getMaximumValue()) {
                tempFromTime = tempFromTime.dayOfMonth().add(((toDate.getMillis() - tempFromTime.getMillis())
                        / ((long) DateTimeConstants.MILLIS_PER_DAY)));
            } else {
                tempFromTime = tempFromTime.monthOfYear().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.DAY.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempToTime.getSecondOfMinute() != 0
                && (tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_SECOND) {
            toDate = tempToTime.copy();
            long remainingSeconds = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_MINUTE) / DateTimeConstants.MILLIS_PER_SECOND;
            if (remainingSeconds < 60) {
                tempToTime = tempToTime.secondOfMinute().add(-1 * remainingSeconds);

            } else {
                tempToTime = tempToTime.secondOfMinute().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getMinuteOfHour() != 0 && ((tempToTime.getMillis()
                - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) {
            toDate = tempToTime.copy();
            long remainingMinutes = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_HOUR) / DateTimeConstants.MILLIS_PER_MINUTE;
            if (remainingMinutes < 60) {
                tempToTime = tempToTime.minuteOfHour().add(-1 * remainingMinutes);
            } else {
                tempToTime = tempToTime.hourOfDay().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.MINUTE.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getHourOfDay() != 0
                && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) {
            toDate = tempToTime.copy();
            long remainingHours = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_DAY) / DateTimeConstants.MILLIS_PER_HOUR;
            if (remainingHours < 24) {
                tempToTime = tempToTime.hourOfDay().add(-1 * remainingHours);
            } else {
                tempToTime = tempToTime.dayOfMonth().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.HOUR.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getDayOfMonth() != 1
                && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) {
            toDate = tempToTime.copy();
            tempToTime = tempToTime.monthOfYear().roundFloor();
            ranges.add(new TimeRange(RangeUnit.DAY.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.isAfter(tempFromTime)) {
            ranges.add(new TimeRange(RangeUnit.MONTH.name(),
                    new long[] { tempFromTime.getMillis(), tempToTime.getMillis() }));
        }
    }
    if (log.isDebugEnabled()) {
        for (TimeRange timeRange : ranges) {
            log.debug("Unit: " + timeRange.getUnit() + " Range: "
                    + formatter.format(new Date(timeRange.getRange()[0])) + "(" + timeRange.getRange()[0]
                    + ")->" + formatter.format(new Date(timeRange.getRange()[1])) + "("
                    + timeRange.getRange()[1] + ")");
        }
    }
    return ranges;
}

From source file:org.wso2.analytics.shared.util.time.TimeRangeUtils.java

License:Open Source License

public static List<TimeRange> getDateTimeRanges(long from, long to) {
    List<TimeRange> ranges = new ArrayList<>(10);
    MutableDateTime fromDate = new MutableDateTime(from);
    fromDate.set(DateTimeFieldType.millisOfSecond(), 0);
    MutableDateTime toDate = new MutableDateTime(to);
    toDate.set(DateTimeFieldType.millisOfSecond(), 0);
    MutableDateTime tempFromTime = fromDate.copy();
    MutableDateTime tempToTime = toDate.copy();

    if (log.isDebugEnabled()) {
        log.debug("Time range: " + formatter.format(fromDate.toDate()) + "->"
                + formatter.format(toDate.toDate()));
    }//from ww w.j a  v a 2 s  .  c  om

    if (toDate.getMillis() - fromDate.getMillis() < DateTimeConstants.MILLIS_PER_MINUTE) {
        ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                new long[] { fromDate.getMillis(), toDate.getMillis() }));
    } else {
        if (tempFromTime.getSecondOfMinute() != 0
                && (toDate.getMillis() - fromDate.getMillis() > DateTimeConstants.MILLIS_PER_MINUTE)) {
            tempFromTime = tempFromTime.minuteOfHour().roundCeiling();
            ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getMinuteOfHour() != 0
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) {
            fromDate = tempFromTime.copy();
            if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE) < 60) {
                tempFromTime = tempFromTime.minuteOfHour().add(
                        (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_MINUTE);
            } else {
                tempFromTime = tempFromTime.hourOfDay().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.MINUTE.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getHourOfDay() != 0
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) {
            fromDate = tempFromTime.copy();
            if (((toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR) < 24) {
                tempFromTime = tempFromTime.hourOfDay().add(
                        (toDate.getMillis() - tempFromTime.getMillis()) / DateTimeConstants.MILLIS_PER_HOUR);
            } else {
                tempFromTime = tempFromTime.dayOfMonth().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.HOUR.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempFromTime.getDayOfMonth() != 1
                && ((toDate.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) {
            fromDate = tempFromTime.copy();
            if ((((toDate.getMillis() - tempFromTime.getMillis())
                    / DateTimeConstants.MILLIS_PER_DAY)) < tempFromTime.dayOfMonth().getMaximumValue()) {
                tempFromTime = tempFromTime.dayOfMonth().add(((toDate.getMillis() - tempFromTime.getMillis())
                        / ((long) DateTimeConstants.MILLIS_PER_DAY)));
            } else {
                tempFromTime = tempFromTime.monthOfYear().roundCeiling();
            }
            ranges.add(new TimeRange(RangeUnit.DAY.name(),
                    new long[] { fromDate.getMillis(), tempFromTime.getMillis() }));
        }
        if (tempToTime.getSecondOfMinute() != 0
                && (tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_SECOND) {
            toDate = tempToTime.copy();
            long remainingSeconds = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_MINUTE) / DateTimeConstants.MILLIS_PER_SECOND;
            if (remainingSeconds < 60) {
                tempToTime = tempToTime.secondOfMinute().add(-1 * remainingSeconds);

            } else {
                tempToTime = tempToTime.secondOfMinute().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.SECOND.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getMinuteOfHour() != 0 && ((tempToTime.getMillis()
                - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_MINUTE)) {
            toDate = tempToTime.copy();
            long remainingMinutes = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_HOUR) / DateTimeConstants.MILLIS_PER_MINUTE;
            if (remainingMinutes < 60) {
                tempToTime = tempToTime.minuteOfHour().add(-1 * remainingMinutes);
            } else {
                tempToTime = tempToTime.hourOfDay().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.MINUTE.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getHourOfDay() != 0
                && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_HOUR)) {
            toDate = tempToTime.copy();
            long remainingHours = ((toDate.getMillis() - tempFromTime.getMillis())
                    % DateTimeConstants.MILLIS_PER_DAY) / DateTimeConstants.MILLIS_PER_HOUR;
            if (remainingHours < 24) {
                tempToTime = tempToTime.hourOfDay().add(-1 * remainingHours);
            } else {
                tempToTime = tempToTime.dayOfMonth().roundFloor();
            }
            ranges.add(new TimeRange(RangeUnit.HOUR.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.getDayOfMonth() != 1
                && ((tempToTime.getMillis() - tempFromTime.getMillis()) >= DateTimeConstants.MILLIS_PER_DAY)) {
            toDate = tempToTime.copy();
            tempToTime = tempToTime.monthOfYear().roundFloor();
            ranges.add(new TimeRange(RangeUnit.DAY.name(),
                    new long[] { tempToTime.getMillis(), toDate.getMillis() }));
        }
        if (tempToTime.isAfter(tempFromTime)) {
            ranges.add(new TimeRange(RangeUnit.MONTH.name(),
                    new long[] { tempFromTime.getMillis(), tempToTime.getMillis() }));
        }
    }
    if (log.isDebugEnabled()) {
        for (TimeRange timeRange : ranges) {
            log.debug("Unit: " + timeRange.getUnit() + " Range: "
                    + formatter.format(new Date(timeRange.getRange()[0])) + "(" + timeRange.getRange()[0]
                    + ")->" + formatter.format(new Date(timeRange.getRange()[1])) + "("
                    + timeRange.getRange()[1] + ")");
        }
    }
    return ranges;
}