Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-17
5
Medium Priority
?
963 Views
Last Modified: 2008-02-01
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());
0
Comment
Question by:SuAeE
5 Comments
 
LVL 2

Accepted Solution

by:
SergeiKo earned 200 total points
ID: 12088073
Hello, SuAeE.

Try add() mthod instead of roll() in Calendar class:

       finishDate.add( Calendar.DAY_OF_YEAR, 365 );


Regards.
0
 
LVL 2

Expert Comment

by:SergeiKo
ID: 12088085
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() );
    }
}
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 12090488
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
0
 
LVL 3

Assisted Solution

by:nesnemis
nesnemis earned 200 total points
ID: 12090646
Hi

You can use finishDate.add(GregorianCalendar.YEAR, 1 );

This way you won't get one day short if it's a leapyear...
0
 

Author Comment

by:SuAeE
ID: 12110435

Thanks for that guys :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question