Re: add N days to yyyyMMdd integer date

Discussion in 'Java' started by Phil Hanna, Jun 30, 2003.

  1. Phil Hanna

    Phil Hanna Guest

    On 22 Jun 2003 06:37:08 -0700, (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
    Phil Hanna, Jun 30, 2003
    #1
    1. Advertising

  2. Phil Hanna

    Roedy Green Guest

    On Mon, 30 Jun 2003 22:59:04 GMT, (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
    Roedy Green, Jul 1, 2003
    #2
    1. Advertising

  3. "Phil Hanna" <> wrote in message
    news:...
    > 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
    Adam P. Jenkins, Jul 1, 2003
    #3
  4. Phil Hanna

    Keeger Guest

    (Phil Hanna) wrote in message news:<>...
    > On 22 Jun 2003 06:37:08 -0700, (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.
    Keeger, Jul 1, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Stockwell
    Replies:
    2
    Views:
    700
    Anna Martelli Ravenscroft
    Sep 20, 2004
  2. Hitesh

    ST_CTIME convert to yyyymmdd

    Hitesh, Aug 7, 2006, in forum: Python
    Replies:
    1
    Views:
    545
    Hitesh
    Aug 7, 2006
  3. Konstantinos Pachopoulos

    find difference in days from YYYYMMDD to YYYYMMDD

    Konstantinos Pachopoulos, Sep 22, 2007, in forum: Python
    Replies:
    5
    Views:
    311
  4. Replies:
    8
    Views:
    436
    Lubna tt
    Jun 26, 2005
  5. Alan Gresley

    With YYYYMMDD Trying to change MM to August

    Alan Gresley, Feb 18, 2008, in forum: Javascript
    Replies:
    7
    Views:
    131
    Thomas 'PointedEars' Lahn
    Feb 22, 2008
Loading...

Share This Page