Lost in "Date" maze

T

Tim Slattery

I'm retrieving a datetime value from my database. I can easily
retrieve this into a java.sql.Date object. Once retrieved, I want to
display the month and year from it in a yyyymm format. I also want to
calculate the four preceding months, and display them likewise. So if
I retrieve a date in September, 2003 from the DB, I want the following
five display strings: 200309, 200308, 200307, 200306, 200305.

So I can use Date.getYear() and Date.getMonth(), then manipulate them,
right? But those functions are deprecated, and we're supposed to use
Calendar.get(Calendar.YEAR) and Calendar.get(Calendar.MONTH).

Ok...Calendar includes a neat add(...) function that I can use to do
date arithmetic. But Calendar.get(Calendar.MONTH) returns one of the
constants defined within Calendar, and they could have any value; so
how do I format the string that I need? Hmm..there's a DateFormat
class....but I can't for the life of me figure out how to make it
yield the string that I need.

So what's the answer? Should I just give up and use the deprecated
functions from java.util.Date (which java.sql.Date extends)?
 
M

Matt Humphrey

Tim Slattery said:
I'm retrieving a datetime value from my database. I can easily
retrieve this into a java.sql.Date object. Once retrieved, I want to
display the month and year from it in a yyyymm format. I also want to
calculate the four preceding months, and display them likewise. So if
I retrieve a date in September, 2003 from the DB, I want the following
five display strings: 200309, 200308, 200307, 200306, 200305.

So I can use Date.getYear() and Date.getMonth(), then manipulate them,
right? But those functions are deprecated, and we're supposed to use
Calendar.get(Calendar.YEAR) and Calendar.get(Calendar.MONTH).

Ok...Calendar includes a neat add(...) function that I can use to do
date arithmetic. But Calendar.get(Calendar.MONTH) returns one of the
constants defined within Calendar, and they could have any value; so
how do I format the string that I need? Hmm..there's a DateFormat
class....but I can't for the life of me figure out how to make it
yield the string that I need.

You're very close to the answer. You want SimpleDateFormat. This class
lets you express the format of a date as a string. The API shows how to use
the formatting characters and gives some examples. To get the 200309 format
for September 2003, you would use

SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
String s = sdf.format (date);

Other formats can be like MM-dd-yyyy HH:mm:ss:SS" for the normal numeric
date of 09-01-2004 05:13:12:78. Using longer format expressions (e.g. MMMM
instead of MM) will insert the word rather than the number.

There are also some nice localization features in the API, such as
DateFormat df = DateFormat.getDateTimeInstance();
String s = df.format( new Date () );

So that different localities get different formats.


Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
K

kaeli

So what's the answer? Should I just give up and use the deprecated
functions from java.util.Date (which java.sql.Date extends)?

No.

java.sql.Date myDB_date = rs.getDate("myDB_field");
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat
("yyyyMM");
String myString = df.format(myDB_date);

You get your format.

df.format takes a date.

Add your dates as calendars, which you figured out how to do from what I
can tell. Then do
myString = df.format(myCalendar.getTime());
Calendar.getTime() returns a date. SimpleDateFormat.format takes one.

--
--
~kaeli~
Going to church doesn't make you a Christian any more than
standing in a garage makes you a car.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
T

Tim Slattery

Matt Humphrey said:
You're very close to the answer. You want SimpleDateFormat. This class
lets you express the format of a date as a string. The API shows how to use
the formatting characters and gives some examples. To get the 200309 format
for September 2003, you would use

SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
String s = sdf.format (date);

OK, thanks. But to do arithmetic on a date, I have to create a
Calendar object from it. Can I use SimpleDateFormat with a Calendar
object?
 
S

Steve W. Jackson

:>
:>:>> I'm retrieving a datetime value from my database. I can easily
:>> retrieve this into a java.sql.Date object. Once retrieved, I want to
:>> display the month and year from it in a yyyymm format. I also want to
:>> calculate the four preceding months, and display them likewise. So if
:>> I retrieve a date in September, 2003 from the DB, I want the following
:>> five display strings: 200309, 200308, 200307, 200306, 200305.
:>>
:>> So I can use Date.getYear() and Date.getMonth(), then manipulate them,
:>> right? But those functions are deprecated, and we're supposed to use
:>> Calendar.get(Calendar.YEAR) and Calendar.get(Calendar.MONTH).
:>>
:>> Ok...Calendar includes a neat add(...) function that I can use to do
:>> date arithmetic. But Calendar.get(Calendar.MONTH) returns one of the
:>> constants defined within Calendar, and they could have any value; so
:>> how do I format the string that I need? Hmm..there's a DateFormat
:>> class....but I can't for the life of me figure out how to make it
:>> yield the string that I need.
:>
:>You're very close to the answer. You want SimpleDateFormat. This class
:>lets you express the format of a date as a string. The API shows how to use
:>the formatting characters and gives some examples. To get the 200309 format
:>for September 2003, you would use
:>
:>SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
:>String s = sdf.format (date);
:
:OK, thanks. But to do arithmetic on a date, I have to create a
:Calendar object from it. Can I use SimpleDateFormat with a Calendar
:eek:bject?
:
:--
:Tim Slattery
:[email protected][/QUOTE]

You can create a Calendar in more than one way (like the static call to
Calendar.getInstance(), or a new GregorianCalendar()), then set its
fields by calling setTime() and passing your Date object. Then the
SimpleDateFormat class has a setCalendar() method which will accept your
Calendar as a parameter. This essentially has to do with locales and
such, and not the date/time to be formatted, so you may not need it.

= Steve =
 
P

P.Hill

Tim,

You can create a Calendar in more than one way (like the static call to
Calendar.getInstance(), or a new GregorianCalendar()), then set its
fields by calling setTime() and passing your Date object. Then the
SimpleDateFormat class has a setCalendar() method which will accept your
Calendar as a parameter. This essentially has to do with locales and
such, and not the date/time to be formatted, so you may not need it.

You can do a SimpleDateFormat.getCalendar()/setCalendar(). It will return you
the calendar it is using and not a copy, but there is no way, or at least there
didn't used to be any way, to get a SimpleDateFormat to read not from a new Date
going in, but from a new Calendar going into the format.

So you might as well not violate any pedantic OO rules by trying to manipulate
a Calendar that is being used by a DateFormat, but instead use one Calendar to
play with the date, then do a myCal.getTime() and push that into
DateFormat.format( date ) for a nice String.
That is what I would do.

Make sure if you do have to deal with TZ etc. in your calculating Calendar
you also set the TZ in the formatter (or the underling calendar), otherwise
you may really confuse yourself when the results are not what you expect.

-Paul
 
G

Giorgio Franceschetti

Ok, but How can I use a JFormattedTextField?
It accepts only Date class... :-(
Giorgio
 
T

Thomas Schodt

Giorgio said:
Ok, but How can I use a JFormattedTextField?
It accepts only Date class... :-(

The answer has already been given (implicitly) in this thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top