Solved

Dates etc

Posted on 1998-07-29
4
185 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 39
jar file executable 12 52
Maven Project: Hibernate Dependencies Conflict 10 26
Glassfish admin console not working 1 16
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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

832 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