Solved

error in Date function

Posted on 2003-10-28
10
334 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 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…
The viewer will learn how to implement Singleton Design Pattern in Java.

828 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