Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now