GregorianCalendar, Date and Java Dating

The problem I have is the following. I have a Date, lets say today and I want to get add, for example, 120 days to get another Date object, which of course should point 120 days into the future.
I know how to parse Strings into Date objects and vice versa. I have done it before a while ago and it worked going about it like this. I get the date. I get a GregorianCalendar and use the setTime(Date d) method to the date and use then the Gregorians add(int, int) method to increment the days. I could also use roll(int, true).
It doesn`t work so. I have tried pretty much everything and the problem is I either get bogus, or the month and years dont get incremented when they should. I used Suns JDK 1.7B Y2K compliant and also 1.1.8. I use NT 4.0 with SP 3. Do you have any idea ? Or some code snippets which work for you. Please also tell me the JDK version and operating system.

Here comes my source

import java.util.*;
import java.text.*;

public class MainDateExamples
{

   public MainDateExamples()
   {
      SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");

      sdf.setLenient(false);

      String d = new String("1.10.1999");

      Date before = sdf.parse(d, new ParsePosition(0));

      GregorianCalendar g = new GregorianCalendar();

      g.setTime(before);

      g.add(GregorianCalendar.DATE, 120);
      // Doesn work either :(  g.add(GregorianCalendar.DAY_OF_YEAR, 120);

      Date after = g.getTime();

      System.out.println("Before = " + d);
      System.out.println("After  = " + sdf.format(after));

      String kkkk = null;
      while(true);
   }

   public final static void main(String args[])
   {
      MainDateExamples md = new MainDateExamples();
   }
}

The output is :

Before = 1.10.1999
After  = 01.10.1999

and the loosser is me since I have no clue why.

What do you think ?
tequillaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ravindra76Commented:

Hi ,

A working sample of mine

      GregorianCalendar calendar = new GregorianCalendar();
      
             int year  =  calendar.get(Calendar.YEAR);
            int month =  calendar.get(Calendar.MONTH);
             int date   =  calendar.get(Calendar.DATE);

            String today = year+"-"+month+"-"+date;
            System.out.println("Today:"+today);
            
            calendar.add(Calendar.DATE,10);
            String nextdate = calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DATE);
            System.out.println("Next date:"+nextdate);

Best of luck
0
Ravindra76Commented:

Your new code:-

import java.util.*;
import java.text.*;

public class MainDateExamples
{

public MainDateExamples()
{
SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");

sdf.setLenient(false);

String d = new String("1.10.1999");

Date before = sdf.parse(d, new ParsePosition(0));
System.out.println("Before:"+sdf.format(before));

GregorianCalendar g = new GregorianCalendar();

g.setTime(before);
System.out.println("Before:"+before);

g.add(g.DATE, 120);
// Doesn work either :( g.add(GregorianCalendar.DAY_OF_YEAR, 120);

Date after = g.getTime();

System.out.println("After:"+after);
System.out.println("Before = " + d);
System.out.println("After = " + sdf.format(after));

String kkkk = null;
while(true);
}

public final static void main(String args[])
{
MainDateExamples md = new MainDateExamples();
}
}


Check the output.

You found after date was updates 4 months.

But g.getTime() is not updates.

But the strange thing is

01.10.1999 is september 1st.
it is showing jan 1st.

SO there may be wrong with parseposition.

Best of luck
0
Ravindra76Commented:

dd.mm.yyyy format.
String d = new String("1.10.1999");

From my code , you observe that

1.10.99 menas not october 1

1- Jan

10: 00:10:00 means time

1999 -- Year

That's why output came as

Fri java 01 00:10:00 GMT +05:30 1999
(I am in india: That's why it is 5:30).

with this you can know that
dd.mm.yyyy format of 1.10.1999 is not october1 but it is january 1 with time
00:10:00

And after adding 120 days,
the output is

sat May 01 00:10:00 GMT +05:30 1999
means 102 days are added properly.


Summary : You are not using proper code.
I don't know it is a bug or not.

So use my frist commnet as solution to increment dates properly.

Best of luck.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

JodCommented:
Try this out for size...

import java.util.*;
import java.text.*;

public class dateTest
{

   static public void addDays(String d, int days)
   {
      SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");
      sdf.setLenient(false);

      Date before = sdf.parse(d, new ParsePosition(0));
      System.out.println("Before = " + sdf.format(before) );

      Date after = new Date();
     
      // add 120 days to date...
      after.setTime( before.getTime() + (1000 * 60 * 60 * 24 * days) );
     
      System.out.println("After " + days + " days = " + sdf.format(after));
     
   }

   public final static void main(String args[])
   {
      addDays("1.10.1999",120);  
   }
}

Date stores time as milliseconds since 1/1/1970 (or somewhere close to that) you can just add the seconds to it to add time to a date.

To add 120 days then you just do...

date1.setTime( date1.getTime() + ( (1000 * 60 * 60 * 24) * numberOfDays) );
0
sen_kumCommented:

  Here is the answer for your question.
SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" )

  To specify the month it's MM , to specify minutes its mm, you have used mm in your program change that to MM , it will work.


import java.util.* ;
import java.text.* ;
public class MainDateExamples
{
  public MainDateExamples()
  {
      SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" ) ;

      sdf.setLenient( false ) ;

      String d = new String( "1.10.1999" ) ;

      Date before = sdf.parse( d, new ParsePosition( 0 ) ) ;

      GregorianCalendar g = new GregorianCalendar() ;

      g.setTime( before ) ;

      g.add( GregorianCalendar.DATE, 120 ) ;
      // Doesn work either :(  g.add(GregorianCalendar.DAY_OF_YEAR, 120);

      Date after = g.getTime() ;

      System.out.println( "Before = " + d ) ;
      System.out.println( "After  = " + sdf.format( after ) ) ;
   }
   public final static void main(String args[])
   {
         MainDateExamples md = new MainDateExamples();
    }
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JodCommented:
Ha ha ha...

I've only just mentioned this in the answer to the question below this one:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10259324 

I just copied the line:

SimpleDateFormat sdf = new SimpleDateFormat("dd.mm.yyyy");

as well without checking...tut tut...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.