Solved

Dates etc

Posted on 1998-07-29
4
175 Views
Last Modified: 2010-03-30
Im looking for a way to take a String convert it to a GregorianCalendar object and add
a number of days to it and return a string
Something like:

String date="01/01/1998";
int month=Integer.parseInt(substring(0,2));
int month=Integer.parseInt(substring(3,5));
int year= Integer.parseInt(substring(6));

Calendar cal= new GregorianCalendar(year,month,day);
cal.roll(Calendar.DATE,true);
for (int i=0;i<10;i++)  {
     cal.add(GregorianCalendar.DATE,7);  //to get a week in the future
     System.out.println("DATE: " + cal.getTime());
     //convert new date to string in MM/DD/YYYY format??????
}

This gives an incorrect date i think because it uses 0 for January, 1 for Feb, etc in the GregorianCalendar constructor
So the output is something like:
DATE: Mon Feb 02 00:00:00 CST 1998
DATE: Mon Feb 09 00:00:00 CST 1998
DATE: Mon Feb 16 00:00:00 CST 1998
DATE: Mon Feb 23 00:00:00 CST 1998
DATE: Mon Mar 02 00:00:00 CST 1998
DATE: Mon Mar 09 00:00:00 CST 1998
DATE: Mon Mar 16 00:00:00 CST 1998
DATE: Mon Mar 23 00:00:00 CST 1998
DATE: Mon Mar 30 00:00:00 CST 1998
DATE: Mon Apr 06 00:00:00 CDT 1998
DATE: Mon Apr 13 00:00:00 CDT 1998
 
Which is not what was intended if i use:
Calendar cal= new GregorianCalendar(year,month,day-1);
it works like i intended but i still have the problem of converting what is returned by getTime() to a format
that I prefer (or better yet that I can alter whether it be MM/DD/YYYY or DD/MM/YYYY or maybe even a two digit year) without having the time show up

I guess what im asking is what is the best way to do this (or even a good way)
The whole Date/Calendar class thing is a bit confusing
anyone out there figure it out and want to share some knowledge?

PS i may left out exception catching
 
0
Comment
Question by:mbunkows
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 50 total points
ID: 1229106
Try this:

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = dateFormat.format(cal.getTime());
0
 

Author Comment

by:mbunkows
ID: 1229107
ahhh java.text package didnt even look in there :(

but what about the converting from string to Calender
do i just use the constructer like:
GregorianCalendar gc=GregorianCalendar(year,month,day-1);  ???
seems a bit illogical
is there a better way?
i guess im just not sure why it works that way

Thanks
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1229108
Check the DateFormat.parse(String date) method which returns the Date object. (It works in reverse top the format() method).
0
 

Author Comment

by:mbunkows
ID: 1229109
works great!
just need to leave the constructers out of it

Thanks alot!

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

744 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

10 Experts available now in Live!

Get 1:1 Help Now