?
Solved

Dates etc

Posted on 1998-07-29
4
Medium Priority
?
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 200 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

741 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