Java Date to Time getTimeRoll(Date current, int field, int numberOfRoll)

Here you can find the source of getTimeRoll(Date current, int field, int numberOfRoll)

Description

get Time Roll

License

Open Source License

Declaration

public static Date getTimeRoll(Date current, int field, int numberOfRoll) 

Method Source Code


//package com.java2s;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static Date getTimeRoll(Date current, int field, int numberOfRoll) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(current);/*from  www  . j av  a  2 s  .c  o  m*/

        if (field == Calendar.DATE) {
            if (calendar.get(Calendar.DATE) == calendar.getActualMinimum(Calendar.DATE)) {
                if (calendar.get(Calendar.MONTH) == calendar.JANUARY) {
                    if (numberOfRoll < 0) {
                        calendar.roll(Calendar.MONTH, -1);
                        calendar.roll(Calendar.YEAR, -1);
                        calendar.roll(field, numberOfRoll);
                    } else {
                        calendar.roll(field, numberOfRoll);
                    }
                } else {
                    if (numberOfRoll < 0) {
                        calendar.roll(Calendar.MONTH, -1);
                        calendar.roll(field, numberOfRoll);
                    } else {
                        calendar.roll(field, numberOfRoll);
                    }
                }

            } else if (calendar.get(Calendar.DATE) == calendar.getActualMaximum(Calendar.DATE)) {
                if (calendar.get(Calendar.MONTH) == calendar.DECEMBER) {
                    if (numberOfRoll > 0) {
                        calendar.roll(Calendar.YEAR, +1);
                        calendar.roll(Calendar.MONTH, +1);
                        calendar.roll(field, numberOfRoll);
                    } else {
                        calendar.roll(field, numberOfRoll);
                    }
                } else {
                    if (numberOfRoll > 0) {
                        calendar.roll(Calendar.MONTH, +1);
                        calendar.roll(field, numberOfRoll);
                    } else {
                        calendar.roll(field, numberOfRoll);
                    }
                }
            } else {

                calendar.roll(field, numberOfRoll);
            }
        }

        if (field == Calendar.MONTH) {
            if ((numberOfRoll + calendar.get(Calendar.MONTH) + 1) <= 0) {
                calendar.roll(Calendar.YEAR, -1);
            }
            if (calendar.get(Calendar.MONTH) == Calendar.JANUARY) {
                if (numberOfRoll < 0) {
                    calendar.roll(field, numberOfRoll);
                    calendar.roll(Calendar.YEAR, -1);
                } else {
                    calendar.roll(field, numberOfRoll);
                }
            } else if (calendar.get(Calendar.MONTH) == Calendar.DECEMBER) {
                if (numberOfRoll > 0) {
                    calendar.roll(field, numberOfRoll);
                    calendar.roll(Calendar.YEAR, 1);
                } else {
                    calendar.roll(field, numberOfRoll);
                }
            } else {
                calendar.roll(field, numberOfRoll);
            }

        }

        return calendar.getTime();
    }
}

Related

  1. getTimeOnly(Date date)
  2. getTimeOnly(final Date oDate)
  3. getTimePart(Date dateObject)
  4. getTimePeriod(Date date)
  5. getTimerDate(String time)
  6. getTimeStamp(long timeMilis, boolean time, boolean date)
  7. getTimeStampInMin(Date date)
  8. getTimeStampInSec(char separator, Date date)
  9. getTimestampl(Date time1, Date time2)