Link to home
Start Free TrialLog in
Avatar of SuAeE
SuAeE

asked on

problem with .roll(Calendar.DAY_OF_YEAR, 365 );

Hi, im using the following code to store a future date in a string, the problem is that when the date goes into a new year the current year remains.
So if I "roll" past december of this year, the year stored in the date will be 2004 instead of 2005. How can I resolve this and changing my code as little as possible. Thanks
 
Calendar date = Calendar.getInstance();
 finishDate.roll(Calendar.DAY_OF_YEAR, 365 );
String Date = sdf.format(date.getTime());
ASKER CERTIFIED SOLUTION
Avatar of SergeiKo
SergeiKo

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
Avatar of SergeiKo
SergeiKo

Whole example for test th difference is.


import java.util.*;

class DateChanger
{
    public static void main( String[] args )
    {
        Calendar date = Calendar.getInstance();
        System.out.println( "1. now=" + date.getTime() );

        date.roll( Calendar.DAY_OF_YEAR, 365 );
        System.out.println( "1. roll=" + date.getTime() );


        date = Calendar.getInstance();
        System.out.println( "2. now=" + date.getTime() );

        date.add( Calendar.DAY_OF_YEAR, 365 );
        System.out.println( "2. add=" + date.getTime() );
    }
}
Hello, SuAeE.
May be you can try this snippet..

import java.util.Calendar;
import java.util.GregorianCalendar;

class calendar
{
  public static void main(String args[])
  {
    Calendar  c = new GregorianCalendar() ;
    int curYear = c.get(GregorianCalendar.YEAR);
    System.out.println(" current Year ="+curYear );
    c.roll(GregorianCalendar.YEAR,1);
    int newYear = c.get(GregorianCalendar.YEAR);
    System.out.println(" current Year ="+ newYear);
  }
}

Bye
CodingExperts
SOLUTION
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
Avatar of SuAeE

ASKER


Thanks for that guys :)