Solved

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

Posted on 2004-09-17
5
935 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Accepted Solution

by:
SergeiKo earned 50 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 50 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java imports not found 4 71
Github api to fetch the number of collaborators in a git repo 1 70
Html split(text) 2 50
Bitwise and to sum elements 2 54
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

752 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