Extracting the year from a Date() object

105 views
Skip to first unread message

Rob Tanner

unread,
Jun 7, 2010, 7:58:57 PM6/7/10
to Google Web Toolkit
I have a Date() object that holds a date of birth and I want to
extract the year and make sure no one's trying to tell me they're 150
years old, etc. The getYear method of the Date() class is deprecated
and you normally use a Calendar() to get the year. The problem is
that when I try to use a Calendar() object the application doesn't
know what it is. How can I extract the year from a Date() object and
not fall back on a deprecated method?

Thanks,
Rob

Jeff Chimene

unread,
Jun 7, 2010, 8:57:03 PM6/7/10
to google-we...@googlegroups.com

Calendar() isn't supported.

Use the Date() class to remain within the GWT framework. If you really
must have robust date handing, you can always call the server.

Eric

unread,
Jun 8, 2010, 10:29:40 AM6/8/10
to Google Web Toolkit
Sadly, the best method is to use GWT's DateTimeFormat:

private static final DateTimeFormat YEAR_FORMAT =
DateTimeFormat.getFormat("yyyy");
public static int year(final Date date) {
return Integer.parseInt(YEAR_FORMAT.format(date));
}

You can put this into a utility class; you might even want to
remove the static modifier on year() and inject the class
where its needed; that might help testing.

Since GWT is being translated to JavaScript, and JS has no
threads, you can leave YEAR_FORMAT static, and you don't
need to synchronize access to it or create one for each
year() call. Still, since you get the format through a getFormat()
call and not a constructor, I suspect GWT caches DateTimeFormats.
In that case the following is clearer.

public int year(Date date) {
return
Integer.parseInt(DateTimeFormat.getFormat("yyyy").format(date));
}

Should we all be using [JG]oda-Time?

Respectfully,
Eric Jablow

Erik Uzureau

unread,
Jun 8, 2010, 11:24:24 AM6/8/10
to google-we...@googlegroups.com
I am curious as to why folks wouldn't just use

        date.getYear() + 1900;

There was a post[1] not to long ago that I replied to (inadvertently 3 times) essentially suggesting the above, but nobody followed up so I'm wondering if I'm not on the right track.

In a nutshell:

1) The GWT date object is based on the js date object
2) The js Date object's getDate(), getYear(), etc  are not deprecated.
3) Official GWT widgets (namely the DatePicker) are using those deprecated methods

Happy to be proven incorrect, but seems more straightforward to me to use those methods than to go through a formatter.

Erik


[1] http://groups.google.com/group/google-web-toolkit/browse_thread/thread/1f04acdede6d9825/713a3dfad374c191?hl=en%CB%89a3dfad374c191



--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


Thomas Broyer

unread,
Jun 10, 2010, 6:31:54 AM6/10/10
to Google Web Toolkit


On 8 juin, 16:29, Eric <erjab...@gmail.com> wrote:
> On Jun 7, 7:58 pm, Rob Tanner <caspersg...@gmail.com> wrote:
>
> > I have a Date() object that holds a date of birth and I want to
> > extract the year and make sure no one's trying to tell me they're 150
> > years old, etc.  The getYear method of the Date() class is deprecated
> > and you normally use a Calendar() to get the year.  The problem is
> > that when I try to use a Calendar() object the application doesn't
> > know what it is.  How can I extract the year from a Date() object and
> > not fall back on a deprecated method?
>
> Sadly, the best method is to use GWT's DateTimeFormat:
>
> private static final DateTimeFormat YEAR_FORMAT =
> DateTimeFormat.getFormat("yyyy");
> public static int year(final Date date) {
>   return Integer.parseInt(YEAR_FORMAT.format(date));
>
> }

Do you really think GWT won't use a Date#getYear() to output the
"yyyy" formatted string? How then parsing it back into an int would be
better than just calling getYear() and adding a @SuppressWarning on
your method?

Eric

unread,
Jun 10, 2010, 11:05:33 AM6/10/10
to Google Web Toolkit
I've come to the conclusion that you're right. I dislike suppressing
warnings; I work on projects where people deprecate their own code
but never refactor the old code away. Suppressing warnings lets those
people hide their malpractice from themselves.

But in this case, the deprecation is Sun's problem. My problem is
that
I'm a programming prig prig. Go ahead--use the anotation, and save
yourself many lines of useless code. If it weren't too much trouble
to
set up, I'd suggest using a JSNI function to do the work. However,
the
invocation and the creation of that are both ugly.

I surrender.

Respectfully,
Eric Jablow

Thomas Broyer

unread,
Jun 10, 2010, 11:58:25 AM6/10/10
to Google Web Toolkit


On 10 juin, 17:05, Eric <erjab...@gmail.com> wrote:
> On Jun 10, 6:31 am, Thomas Broyer <t.bro...@gmail.com> wrote:
>
> > On 8 juin, 16:29, Eric <erjab...@gmail.com> wrote:
> > > Sadly, the best method is to use GWT's DateTimeFormat:
>
> > > private static final DateTimeFormat YEAR_FORMAT =
> > > DateTimeFormat.getFormat("yyyy");
> > > public static int year(final Date date) {
> > >   return Integer.parseInt(YEAR_FORMAT.format(date));
>
> > > }
>
> > Do you really think GWT won't use a Date#getYear() to output the
> > "yyyy" formatted string? How then parsing it back into an int would be
> > better than just calling getYear() and adding a @SuppressWarning on
> > your method?
>
> I've come to the conclusion that you're right.  I dislike suppressing
> warnings; I work on projects where people deprecate their own code
> but never refactor the old code away.  Suppressing warnings lets those
> people hide their malpractice from themselves.

...or you can use the new JsDate (in 2.1M1) ;-)
http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/core/client/JsDate.html
(if you don't have to send it with GWT-RPC or use shared client/server
code)
Reply all
Reply to author
Forward
0 new messages