Solved

error in Date function

Posted on 2003-10-28
10
332 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 50 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 134
Java passing arguments (type error) 2 48
Cipher Configuration on Apache HTTPD 4 53
varialbe initialization 11 30
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 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

22 Experts available now in Live!

Get 1:1 Help Now