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

x
?
Solved

error in Date function

Posted on 2003-10-28
10
Medium Priority
?
338 Views
Last Modified: 2012-05-04
Hi
I am using date function as I have to display the date and day of one week starting from today in one comboBox and adding 6 months to this date in another combobox(as in MS project Information for Periodic Process)
For this I am using the following function which returns me a day and date, but the day returned is false.
Can anyone help me out or provide me any other simple answer

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

for(int i = 0; i<7; i++)
    jComboBox11.addItem(getDate(new Date(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH)+6, cal.get(Calendar.DAY_OF_MONTH)+i ), "E dd.MM.yy"));

public String getDate(Date dt, String format) {
    Date d = new Date(dt.getTime());
    if(format== null)
        format = "dd.MM.yyyy";
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);

    return sdf.format(d);
  }

Thanks Balap
0
Comment
Question by:balap
  • 6
  • 4
10 Comments
 

Author Comment

by:balap
ID: 9634264
when the day is Tuesday it returns me wednesday and so on. one day added to the present day of the week.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 200 total points
ID: 9634753
Can you add 6 months to this month (10) giving the 16th month, and call that a valid date?

I *think* you are trying to create a date that is the 28.16.2003 which is a bit wrong...

Try:

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

cal.add( Calendar.MONTH, 6 ) ;
for( int i = 0 ; i < 7 ; i++ )
{
  jComboBox11.addItem( getDate( new Date( cal.getTimeInMillis() ), "E dd.MM.yy" ) ) ;
  cal.add( Calendar.DAY_OF_YEAR, 1 ) ;
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9634774
Hang on...

it is adding 6 months...

the 28th of April (which is six months away) IS a Wednesday...

0
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.

 
LVL 35

Expert Comment

by:TimYates
ID: 9634782
Just because today is a tuesday, does not mean that in 6 months time it is a tuesday too...

Months are non-uniform (as are years)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9634890
WHat you *might* be meaning to do, is add 26 weeks instead of 6 months...

Calendar cal = Calendar.getInstance(TimeZone.getDefault());

cal.add( Calendar.WEEK_OF_YEAR, 26 ) ;
for( int i = 0 ; i < 7 ; i++ )
{
  jComboBox11.addItem( getDate( new Date( cal.getTimeInMillis() ), "E dd.MM.yy" ) ) ;
  cal.add( Calendar.DAY_OF_YEAR, 1 ) ;
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9634899
Which gives you Tuesday the 27th April...

Not 6 months, but near enough, and the same day as today...
0
 

Author Comment

by:balap
ID: 9640037
Hi I tested it for the present date also. but it gives me thursday althought it is wednesday today.
  for(int i = 0; i<7; i++)
    jCBStartDate.addItem(getDate(new Date(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)+i ), "E dd.MM.yy"));
0
 

Author Comment

by:balap
ID: 9640072
Thanks for the answer. i give u full points
0
 

Author Comment

by:balap
ID: 9640099
anyway this date(int yy, int mm, int dd) constructor has been depricated. your answer has solved both the answers but i coulnt understand why its giving me wrong day inspite of the right date.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9640357
The day was right too :-)

It's just that 6 months from today (Wednesday) is a Thursday...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

926 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