Solved

Dates etc

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

912 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

18 Experts available now in Live!

Get 1:1 Help Now