I was updating some plugins for a project and came to a query for an e-mail program I was writing, I had to send out recently introduced defects to their respective owners and insure that they would only get the defects from the a given offset. It turned out that my API had a XMLGregorianCalendar argument. So I went out to look for a way to offset the date object so I could retrieve the correct information.
I used the following to convert my date:
package com.etechtips;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
public class UpdateDate {
public static void main(String[] args) {
XMLGregorianCalendar xgcstart;
GregorianCalendar gc;
int daycount = 0;
if (args.length != 1)
{
System.out.println("Need to specify offset");
System.exit(1);
}
daycount = Integer.parseInt(args[0]);
// This will get the current date based on System time of an object
gc = new GregorianCalendar();
System.out.println("CURRENT:" + gc.toString());
// This will offset the date by daycount days
gc.add(Calendar.DATE, daycount);
System.out.println("OFFSET: "+ gc.toString());
try
{
// Now the XMLGregorianCalendar object has an
instance that is based on the time set in the gc object
xgcstart = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
}
catch(DatatypeConfigurationException dce)
{
dce.printStackTrace();
System.exit(1);
}
// Code omitted
}
}
Output:
CURRENT:java.util.GregorianCalendar[time=1321793315934,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Chicago",offset=-21600000,
dstSavings=3600000,useDaylight=true,transitions=235,
lastRule=java.util.SimpleTimeZone[id=America/Chicago,offset=-21600000,dstSavings=3600000,
useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,
startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,
endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=10,
WEEK_OF_YEAR=48,WEEK_OF_MONTH=4,DAY_OF_MONTH=20,DAY_OF_YEAR=324,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=3,
AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=48,SECOND=35,MILLISECOND=934,ZONE_OFFSET=-21600000,
DST_OFFSET=0]
OFFSET: java.util.GregorianCalendar[time=1321706915934,areFieldsSet=true,areAllFieldsSet=true,
lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Chicago",offset=-21600000,
dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/Chicago,
offset=-21600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,
startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,
endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,
YEAR=2011,MONTH=10,WEEK_OF_YEAR=47,WEEK_OF_MONTH=3,DAY_OF_MONTH=19,DAY_OF_YEAR=323,
DAY_OF_WEEK=7,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=48,SECOND=35,
MILLISECOND=934,ZONE_OFFSET=-21600000,DST_OFFSET=0]
The printouts above show the current date and the new date based on the offset. I made the day of the month bold in the printouts to show what the offset has updated the date. You could put a much larger offset than 1 and Calendar object will take care of updating the day of week, week of year and more. You will not need to deal with checking for leap year or dealing with updating all the month and years, this is all taken care of for you.
Tags: Java, Programming