Link to home
Start Free TrialLog in
Avatar of tequilla
tequilla

asked on

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 ?
Avatar of Ravindra76
Ravindra76


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

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

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.
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) );
ASKER CERTIFIED SOLUTION
Avatar of sen_kum
sen_kum

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ha ha ha...

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

https://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...