So many things in applications are date specific. Make a booking for 10 days in the future; what is the date going to be in 8 months time; close this account in 5 years time; etc… Dealing with dates can be quite painful, and RFT being Java-based, it is made even more painful. The way java handles dates is very confusing so here’s something to help you get started. The following class provides methods for giving the current date, the date in n days, months or years (past and future). To use it just copy it into a new class (java class, not RFT class) called DateManager and fix up the package line.
/*
* Copyright (c) 2006, Nathaniel Ritmeyer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
* - Neither the name of natontesting.com nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//set the following package path to the correct value
package Scratch;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateManager {
SimpleDateFormat dateFormat; //set the value for this in the constructor to define what date format to return...
public static void main(String args[]) throws Exception
{
DateManager dm = new DateManager();
}
/**
* Sets up a new DateManager, and defines the date format to be used
* @throws Exception
*/
public DateManager() throws Exception
{
dateFormat = new SimpleDateFormat("dd/MM/yyyy");
//See the following for the formats: http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
/*
System.out.println("Current Date: " + getCurrentDate());
System.out.println("Date in 1 day: " + dateInNYearsTime(1));
System.out.println("Date in 10 days: " + dateInNYearsTime(10));
System.out.println("Date in 200 days: " + dateInNYearsTime(200));
System.out.println("Date in 1000 days: " + dateInNYearsTime(1000));
*/
}
/**
* Calculates the current date and returns it as a string in the format defined above
* @return --a string containing today's date
*/
public String getCurrentDate()
{
Date myDate = new Date();
return dateFormat.format(myDate);
}
/**
* Returns a date modified by the iDays parameter relative to the current date
* @param sDays --string value of how many days to add/subtract from the current date
* @return --a string value of the calculated date
* @throws Exception
*/
public String dateInNDaysTime(int iDays) throws Exception
{
Calendar cMyCalendar = Calendar.getInstance();
cMyCalendar.add(Calendar.DATE, iDays);
return dateFormat.format(cMyCalendar.getTime());
}
/**
* Returns a date modified by the iMonths parameter relative to the current date
* @param sMonths --string value of how many months to add/subtract from the current date
* @return --a string value of the calculated date
* @throws Exception
*/
public String dateInNMonthsTime(int iMonths) throws Exception
{
Calendar cMyCalendar = Calendar.getInstance();
cMyCalendar.add(Calendar.MONTH, iMonths);
return dateFormat.format(cMyCalendar.getTime());
}
/**
* Returns a date modified by the iYears parameter relative to the current date
* @param sYears --string value of how many years to add/subtract from the current date
* @return --a string value of the calculated date
* @throws Exception
*/
public String dateInNYearsTime(int iYears) throws Exception
{
Calendar cMyCalendar = Calendar.getInstance();
cMyCalendar.add(Calendar.YEAR, iYears);
return dateFormat.format(cMyCalendar.getTime());
}
}
The example System.out.println(…)s should call dateInNDaysTime(…) instead of dateInNYearsTime(…)
regards