Consultor Eletrônico



Kbase 17950: Apptivity: Manipulating dates : How-to add days to a date
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   5/19/1998
Apptivity: Manipulating dates : How-to add days to a date

INTRODUCTION:
=============
Sometimes it is needed to calculate a due-date. This means that you
need to add some days to a certain date. Here follows some Java code
that performs this.

java.util.Date retVal = new java.util.Date();

//Setting up a time-zone
String[] ids =java.util.TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
java.util.SimpleTimeZone pdt =
new java.util.SimpleTimeZone(1 * 60 * 60 * 1000, ids[0]);
// set up rules for daylight savings time
pdt.setStartRule(java.util.Calendar.APRIL, 1,
java.util.Calendar.SUNDAY, 2 * 60 * 60 * 1000);
pdt.setEndRule(java.util.Calendar.OCTOBER, -1,
java.util.Calendar.SUNDAY, 2 * 60 * 60 * 1000);

java.util.Calendar calendar =
new java.util.GregorianCalendar(pdt,java.util.Locale.UK);
calendar.setTime(retVal);

// Now add 5 days to the date.
// To substract 5 days, insert -5 as the argument
calendar.add(java.util.Calendar.DATE,5);

// Code I used to convert the individual fields back to a date-field.

java.text.SimpleDateFormat CheckFormat
= new java.text.SimpleDateFormat ("dd/MM/yyyy",java.util.Locale.UK);

CheckFormat.setLenient(false);
String dateString = calendar.get(java.util.Calendar.DATE) + "/"
+ (calendar.get(java.util.Calendar.MONTH) + 1) + "/"
+ calendar.get(java.util.Calendar.YEAR);

try{
retVal = CheckFormat.parse(dateString);
}
catch(java.text.ParseException p){
System.out.println("Wrong Parsing");
}
catch(java.lang.IllegalArgumentException d){
System.out.println("The date is wrong");
}
catch(Exception e){
e.printStackTrace();
}


Retval contains now the date + 5


TBO
05/19/1998