Here you can find the source of getTimeRoll(Date current, int field, int numberOfRoll)
public static Date getTimeRoll(Date current, int field, int numberOfRoll)
//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(); } }