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.D AY_OF_YEAR , 365 );
String Date = sdf.format(date.getTime()) ;
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.D
String Date = sdf.format(date.getTime())
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello, SuAeE.
May be you can try this snippet..
import java.util.Calendar;
import java.util.GregorianCalenda r;
class calendar
{
public static void main(String args[])
{
Calendar c = new GregorianCalendar() ;
int curYear = c.get(GregorianCalendar.YE AR);
System.out.println(" current Year ="+curYear );
c.roll(GregorianCalendar.Y EAR,1);
int newYear = c.get(GregorianCalendar.YE AR);
System.out.println(" current Year ="+ newYear);
}
}
Bye
CodingExperts
May be you can try this snippet..
import java.util.Calendar;
import java.util.GregorianCalenda
class calendar
{
public static void main(String args[])
{
Calendar c = new GregorianCalendar() ;
int curYear = c.get(GregorianCalendar.YE
System.out.println(" current Year ="+curYear );
c.roll(GregorianCalendar.Y
int newYear = c.get(GregorianCalendar.YE
System.out.println(" current Year ="+ newYear);
}
}
Bye
CodingExperts
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for that guys :)
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() );
}
}