Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Date: Can you have one that is just mm/dd or mm/yyyy etc?

Thread view: 
LibbyChantel - 25 Apr 2006 23:02 GMT
I need to store some data in this format: "mm/yyyy" as a Date or
DateTime. I can create SimpleDateFormat("mm/yyyy"), but when I do
format.parse() on "12/2001" it helpfully returns "December 1, 2001". I
need to store it as a DateTime of December 2001. Storing it as a String
is not an option.

Has anyone dealt with this kind of situation before and could point me
in the right direction to understand how to handle this?

Thanks,
Libby
Rhino - 26 Apr 2006 00:57 GMT
>I need to store some data in this format: "mm/yyyy" as a Date or
> DateTime. I can create SimpleDateFormat("mm/yyyy"), but when I do
> format.parse() on "12/2001" it helpfully returns "December 1, 2001". I
> need to store it as a DateTime of December 2001. Storing it as a String
> is not an option.

Why is storing the date as a String not an option?

I think you'll need to go that route. Or you could put the month name in a
String and the year in an int and then bundle them together in a new Class
with a name like MonthYear; that class could have methods to get the year
and the month individually for convenience and to format the value however
you want it.

Another way to go would be store an arbitrary day with your month and year
and then just ignore the day portion but I don't much like that; it feels
like bad design to me although I can't articulate exactly what is wrong with
it.

> Has anyone dealt with this kind of situation before and could point me
> in the right direction to understand how to handle this?

This is a pretty unusual requirement, at least in my experience. In my view,
a date always includes a year, month and a day of the month; I wouldn't even
call the combination of year and month alone a date, really.

In business, you normally store a year, month and a day for every 'event',
like a bill payment or an insurance claim. Your reports may end up
summarizing by year and month but then the month/year combination would
normally be stored as a String, if it was stored at all. At least that's
been my experience.

Sorry I couldn't tell you what you wanted to her. Maybe someone else will
have a different take on this question.

--
Rhino
Roedy Green - 26 Apr 2006 06:02 GMT
>Has anyone dealt with this kind of situation before and could point me
>in the right direction to understand how to handle this?

You  can store the date as a long millis since 1970 or int days since
1970 and use Date, GregorianCalendar and SimpleDateFormat when you
need to display it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Thomas Weidenfeller - 26 Apr 2006 07:33 GMT
> I need to store some data in this format: "mm/yyyy" as a Date or
> DateTime.

No, you don't. You need to display them as mm/yyyy. Store the date in
milliseconds since the epoch, and use a display format which suppresses
the day when you display the value.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

LibbyChantel - 26 Apr 2006 13:35 GMT
Unfortunately, yes, I do. At some point the stored date will be
embedded in SQL and, if it is mm/yyyy format the SQL will be "LIKE
%theMm/yyyyDate%", and if it is mm/dd/yyyy format the SQL will be
"=theMm/dd/yyyyDate". I will need to evaluate the stored "Date" to
determine if a day exists in it, and build the SQL accordingly. Storing
the Date as a String would of course be the way to handle this, except
that the Date Object is already previously used in this application I
am patching. I don't wanna rewrite the whole thing...
Thomas Weidenfeller - 26 Apr 2006 15:10 GMT
> Unfortunately, yes, I do.

No, you don't. You are mixing up the representation of a value, and the
value itself.

But since this is getting boring. May I suggest that you at least think
about the above sentence for, hmm, lets say 10 seconds, and study the
API documentation of SimpleDateFormat?

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

LibbyChantel - 26 Apr 2006 15:33 GMT
OK, you're right I don't.

I studied the API doc for SimpleDateFormat before submitting my plea
for help. I guess my real question is, how do I know if a Date portion
such as day is not a valid value for my application, ie when Date =
01/05/2001, how do I know to ignore the month portion (or day, or year)
because it is only a placeholder? If I try to create a Date with
01/00/2001 or some such, I of course get an unparseableDate exception.

I apologize for boring you, I am not nearly as smart as most readers of
this forum, which is why I came to it for help when I couldn't find a
way in the API. Hopefully I will come away from here smarter after
partaking of the comp.lang.java.programmer fountain of knowlege.
Bjorn Abelli - 26 Apr 2006 16:18 GMT
"LibbyChantel" wrote...

> "Thomas Weidenfeller" wrote...

>> "LibbyChantel" wrote...

>  Unfortunately, yes, I do.

>> No, you don't. You are mixing up the representation of a value,
>> and the value itself.

> OK, you're right I don't.
>
[quoted text clipped - 6 lines]
> with 01/00/2001 or some such, I of course get an
> unparseableDate exception.

I think you still need to think over what Thomas said; to realize that
there's a difference between a value and its representation.

A Date is internally represented by a long (number of milliseconds since
since January 1, 1970, 00:00:00 GMT), but as such it can only have values
that corresponds to valid times and dates. There's no way around that.

You actually provided half the answer to the solution yourself in a previous
post.

You want to format an SQL statement (which is represented by a *String*):

"LIKE %theMm/yyyyDate%"

...which means that what you put into that string should be a string itself.

In order to provide the *format* of that part of the string, you simply use
a SimpleDateFormat to format a valid Date into the String representation you
like, even excluding the day part.

That is what you want to put into your SQL statement, not the Date itself.

// Bjorn A
LibbyChantel - 26 Apr 2006 16:26 GMT
You guys are totally missing the point. My question is not how to
format a date to use it in SQL, which is why I left that information
out originally. My question was, is there a way to save only a portion
of a date in a Date object?

My solution is to extend the Date Object and add a field to it which
denotes whether the entire date is valid, or just some combination of
mm/yyyy, etc. But I thought maybe there was a way to use the original
Date object without "enhancing" it. I am REQUIRED to store the date in
a Date, even if it is invalid  : )

Thanks,
Libby
Martin Gregorie - 26 Apr 2006 18:50 GMT
> You guys are totally missing the point. My question is not how to
> format a date to use it in SQL, which is why I left that information
[quoted text clipped - 6 lines]
> Date object without "enhancing" it. I am REQUIRED to store the date in
> a Date, even if it is invalid  : )

Last time I did that sort of thing was in a system that needed to deal
with ordinary dates, financial periods and birth/death dates of
composers and dates of compositions, which are not always known closer
than a year, a decade or even a century.  I held two fields:
    Format code
    Date value

The format code was set by parsing the input date value and was used to
format output so the output looked the same as the input. It could also
be used for validation if only certain date formats were permitted in
some date fields.

The date value was held internally as 8 digits ordered as ccyymmdd so
any format of date could be compared with any other and arbitrary
collections of dates could be sorted. If the input and display format
didn't include a month and/or day the relevant parts of the internal
value  were set to appropriate values that corresponded to valid dates.
Some of the types of date that the system were required to accept were:

Format        Example        Internal form

calendar date     26/04/05    20050426
a month        May 1999    19990501
financial qtr    3rd q 2001    20010701
a year        1492        14920101
a century    19th        18000101
BC year        55BC        -00550101

It must have worked OK: the system was in use for 20 years until it was
rewritten to migrate it from ICL 2900 hardware to a UNIX RDBMS system,
having slid through the change of century with no problems at all.

With the possible exception of BC dates you could do the same but handle
the internal date value as a Date class or an SQL date.

HTH

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Bjorn Abelli - 26 Apr 2006 23:20 GMT
"LibbyChantel" wrote...
> You guys are totally missing the point.

Well, you just didn't express your point very clear. ;-)

We *did* answer your question, but maybe we would have phrased the answers a
bit differently if you had stated your point in another way.

> My question was,
> is there a way to save only a portion
> of a date in a Date object?

As we've said, the date object *always* has a long which represents a valid
date, there's no way around that.

> My solution is to extend the Date Object and add a field
> to it which denotes whether the entire date is valid, or
> just some combination of mm/yyyy, etc. But I thought maybe
> there was a way to use the original Date object without
> "enhancing" it. I am REQUIRED to store the date in
> a Date, even if it is invalid  : )

I'm not sure I dare to ask you why? ;-)

What would the difference be, towards a solution where you combine arbitrary
Date objects with specialized SimpleDateFormats?

Maybe if you could tell us more what those dates are supposed to *do*?

On another note, similar things has been done, even within the library.

e.g. java.sql.Date and java.sql.Time are subclasses of java.util.Date, where
the time respectively the date part is "hidden".

So if it *really* is required to store invalid dates, that's one way to go,
though it seems almost simpler to just create a completely new class, as it
tends to be pretty ugly to break the Liskov substitution principle...

Not knowing what you actually need it for, I would rather suggest using
Composition instead of Inheritance, in order to only expose the necessary
features.

// Bjorn A
Oliver Wong - 27 Apr 2006 18:46 GMT
> You guys are totally missing the point.

   I'm not totally following either.

> My question is not how to
> format a date to use it in SQL, which is why I left that information
> out originally. My question was, is there a way to save only a portion
> of a date in a Date object?

   No, there isn't.

> My solution is to extend the Date Object and add a field to it which
> denotes whether the entire date is valid, or just some combination of
> mm/yyyy, etc But I thought maybe there was a way to use the original
> Date object without "enhancing" it. I am REQUIRED to store the date in
> a Date, even if it is invalid  : )

   Careful the way you phrase this. You say you're "required to store the
date in a Date", but might your requirements be "store the date", without
regards of the actual class where the Date is stored?

   If you really do need to use the Date object, you're out of luck. The
date object simply doesn't support what you want. Even if you extend the
Date object to add functionalities, when you pass that Date object to an SQL
database, the database isn't going to know about your extended
functionalities, and will simply store Dates the way it always has.

   You should probably go a level higher in abstraction, and describe what
you're trying to do, rather than describe how you want to do it.

   - Oliver
Roedy Green - 27 Apr 2006 22:46 GMT
> My question was, is there a way to save only a portion
>> of a date in a Date object?
>
>    No, there isn't.

You can use a convention, e.g. story YYYY-MM as if it were YYYY-MM-01.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

P.Hill - 28 Apr 2006 06:01 GMT
> You can use a convention, e.g. story YYYY-MM as if it were YYYY-MM-01.

At first I thought both Roedy and Thomas were oh so wrong, but I realize
that they read the original quote:

>some data in this format: "mm/yyyy"

Meaning that the OP has a column of date all of which only has month
resolution.  If so, they are right, just display it with less resolution
might be your ticket.  If "some" applied to some of the rows in one
column then Libby is SOL, because 01/01/2006 00:00:00.00 stored in
either a java.util.Date or a DB DATETIME needs something else to tell us
if it is exactly that time or trying to represent the whole month of
January or even the whole year.

Others talked about an additional column which sort of implied that
they were thinking of there was mixed resolutions in one DATETIME column.

Libby, here are some things to consider:

>but when I do format.parse() on "12/2001" it helpfully returns
>December 1, 2001".

No, it returns a java.util.Date which when you call the toString()
either explicitly or when you are using System.out.print(myDate),
shows a millisecond value converted to localized String.  Go back to
this code and consider always showing it to yourself while debugging and
to the user using a SimpleDateFormat as several others have
pointed out.  If this consistency of simply ignoring the smaller fields
works your in business.

One thing to watch is to NOT take my suggestion and print out everything
in the Date wherever you have it in application (particularly right
before displaying to the user and right before shipping to the DB)
and make sure 2005-04-01 T 00:00:00.0000 (using ISO 8601 notation to
avoid any month day confusion in this discussion), doesn't magically
drift to 2005-01-04 T 15:34:17.1234 because you ran the application in
the afternoon and somehow you transfered the right date over the
present date time (just past 3:30 in the afternoon).

Meanwhile, your suggested use of SQL where you
"I will need to evaluate the stored "Date" to
determine if a day exists in it, and build the SQL accordingly. "
seem suspiciously unsual to me. I don't get how the word "the"
is imbedded in a real DATETIME as %theMm/yyyyDate%" and how you can use
the % operator on it.  In fact, I'd be interested in knowing what DB you
are using and what SQL you can use. I dont' thing you want to have the
DB think in terms of Strings, but use SQL Date creating expressions to
specify the appropriate midnight-fixed DATETIME.  Particularly watch
for comparing to not right NOW, but today at midnight with consideration
for when you want this mornings midnight or tonights.

Good luck, and don't be discouraged by people answering the
wrong question.  Hopefully, I have answered the right question.
If not do let me know.

-Paul
Roedy Green - 28 Apr 2006 20:33 GMT
On Thu, 27 Apr 2006 22:01:09 -0700, "P.Hill"
<goodhillREMOVE@xmissionREMOVE.com> wrote, quoted or indirectly quoted
someone who said :

> >some data in this format: "mm/yyyy"
another way to store it is as  binary int of the form yyyymm

you use %100 or /100 to get the fields.

In that form you can still compare date order and equality.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.