Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: add N days to yyyyMMdd integer date (http://www.velocityreviews.com/forums/t123886-re-add-n-days-to-yyyymmdd-integer-date.html)

Phil Hanna 06-30-2003 10:59 PM

Re: add N days to yyyyMMdd integer date
 
On 22 Jun 2003 06:37:08 -0700, jungewum@yahoo.com.au (June Moore)
wrote:

>Hi,
>
>I have a date value (in the form of an integer) yyyyMMdd.
>I would like to add 5 days to it and get back a new date value yyyyMMdd.
>
>e.g. 20030628 + 5 = 20030703
>
>Any help on how this method will be like is greatly appreciated.
>
>Regards.....
>June


Three steps:

1. Convert the integer to a Calendar object
2. Use the Calendar.add method to add days
3. Convert the Calendar object back to the integer format:

import java.io.*;
import java.net.*;
import java.util.*;

public class DateAdd
{
public static void main(String[] args)
{
int date = 20030628;
Calendar cal = dateToCalendar(date);
cal.add(Calendar.DATE, 5);
date = calendarToDate(cal);
System.out.println(date);
}

public static Calendar dateToCalendar(int date) {
int day = date % 100;
int month = (date/100) % 100 - 1;
int year = date / 10000;
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
return cal;
}

public static int calendarToDate(Calendar cal) {
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH) + 1;
int year = cal.get(Calendar.YEAR);
return year * 10000 + month * 100 + day;
}
}

--
Phil Hanna
Author of JSP 2.0: The Complete Reference
http://www.philhanna.com
http://www.philhanna.com

Roedy Green 06-30-2003 11:57 PM

Re: add N days to yyyyMMdd integer date
 
On Mon, 30 Jun 2003 22:59:04 GMT, pehanna@yahoo.com (Phil Hanna) wrote
or quoted :

>>I have a date value (in the form of an integer) yyyyMMdd.
>>I would like to add 5 days to it and get back a new date value yyyyMMdd.
>>
>>e.g. 20030628 + 5 = 20030703
>>
>>Any help on how this method will be like is greatly appreciated.
>>
>>Regards...


If Calendar is too daunting, try BigDate at
http://mindprod.com/products.html#BIGDATE


Adam P. Jenkins 07-01-2003 02:13 AM

Re: add N days to yyyyMMdd integer date
 
"Phil Hanna" <pehanna@yahoo.com> wrote in message
news:3f00c07e.243013655@news.ipass.net...
> Three steps:
>
> 1. Convert the integer to a Calendar object
> 2. Use the Calendar.add method to add days
> 3. Convert the Calendar object back to the integer format:
>
> import java.io.*;
> import java.net.*;
> import java.util.*;
>
> public class DateAdd
> {
> public static void main(String[] args)
> {
> int date = 20030628;
> Calendar cal = dateToCalendar(date);
> cal.add(Calendar.DATE, 5);
> date = calendarToDate(cal);
> System.out.println(date);
> }
>
> public static Calendar dateToCalendar(int date) {
> int day = date % 100;
> int month = (date/100) % 100 - 1;
> int year = date / 10000;
> Calendar cal = Calendar.getInstance();
> cal.set(year, month, day);
> return cal;
> }


You should be using java.text.SimpleDateFormat to parse date strings.
Here's how.

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateAdd
{
private static final SimpleDateFormat dateFmt = new
SimpleDateFormat("yyyyMMdd");

/** Converts a string in the format yyyyMMdd to a Calendar. Throws
ParseException
* if dateStr is not in the correct format. */
public static Calendar dateToCalendar(String dateStr) throws
java.text.ParseException {
Date date = dateFmt.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
}

Adam




Keeger 07-01-2003 04:10 AM

Re: add N days to yyyyMMdd integer date
 
pehanna@yahoo.com (Phil Hanna) wrote in message news:<3f00c07e.243013655@news.ipass.net>...
> On 22 Jun 2003 06:37:08 -0700, jungewum@yahoo.com.au (June Moore)
> wrote:
>
> >Hi,
> >
> >I have a date value (in the form of an integer) yyyyMMdd.
> >I would like to add 5 days to it and get back a new date value yyyyMMdd.
> >
> >e.g. 20030628 + 5 = 20030703
> >
> >Any help on how this method will be like is greatly appreciated.
> >
> >Regards.....
> >June

>
> Three steps:
>
> 1. Convert the integer to a Calendar object
> 2. Use the Calendar.add method to add days
> 3. Convert the Calendar object back to the integer format:
>
> import java.io.*;
> import java.net.*;
> import java.util.*;
>
> public class DateAdd
> {
> public static void main(String[] args)
> {
> int date = 20030628;
> Calendar cal = dateToCalendar(date);
> cal.add(Calendar.DATE, 5);
> date = calendarToDate(cal);
> System.out.println(date);
> }
>
> public static Calendar dateToCalendar(int date) {
> int day = date % 100;
> int month = (date/100) % 100 - 1;
> int year = date / 10000;
> Calendar cal = Calendar.getInstance();
> cal.set(year, month, day);
> return cal;
> }
>
> public static int calendarToDate(Calendar cal) {
> int day = cal.get(Calendar.DATE);
> int month = cal.get(Calendar.MONTH) + 1;
> int year = cal.get(Calendar.YEAR);
> return year * 10000 + month * 100 + day;
> }
> }



There is a bigDate class available from mindprod...I find it useful
for calculating the number of days difference between two dates.


All times are GMT. The time now is 07:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.