Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dates etc

Posted on 1998-07-29
4
Medium Priority
?
191 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

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.

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

636 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