Solved

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

Posted on 2004-09-17
5
928 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse shortcuts 9 55
expectj telnet failing 5 36
MySqlDump not dumping triggers 1 31
servlet web applications   metadata-complete="true" or false 3 40
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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

773 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