Re: add N days to yyyyMMdd integer date

P

Phil Hanna

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;
}
}
 
A

Adam P. Jenkins

Phil Hanna said:
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
 
K

Keeger

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top