How to get the year of the current date ? Date manipulation ...

1,576 views
Skip to first unread message

tim

unread,
Mar 19, 2010, 4:04:48 AM3/19/10
to Google Web Toolkit
Hi all,

I would like to get the year of the current date, but this is not
working :

int year = Calendar.getInstance().get(Calendar.YEAR);

How to use date in gwt ?

Thanks in advance for your answer

Joe Cole

unread,
Mar 19, 2010, 4:32:56 AM3/19/10
to Google Web Toolkit
new Date()

:)

Joe Cole

unread,
Mar 19, 2010, 4:33:28 AM3/19/10
to Google Web Toolkit
Apologies...

int year = new Date().getYear()

On Mar 19, 9:04 pm, tim <timor.su...@gmail.com> wrote:

tim

unread,
Mar 19, 2010, 4:47:43 AM3/19/10
to Google Web Toolkit
Thanks for your answer.

this is marked as deprecated and it's not the good value, that's not
2010.
I would rather use a non-deprecated method.

mariyan nenchev

unread,
Mar 19, 2010, 4:52:54 AM3/19/10
to google-we...@googlegroups.com
Unfortunately GWT does not have support for Calendar class from the java api. I think you may use gwt DateTimeFormat and pass to it custom format with only the year(not sure if this is possible), and you will get the year as String.

Thomas Broyer

unread,
Mar 19, 2010, 11:33:48 AM3/19/10
to Google Web Toolkit

On Mar 19, 9:47 am, tim <timor.su...@gmail.com> wrote:
> Thanks for your answer.
>
> this is marked as deprecated and it's not the good value, that's not
> 2010.

int year = 1900 + new Date().getYear();

> I would rather use a non-deprecated method.

A future version of GWT (that's already in trunk, just not yet
released) will have a JsDate class to wrap the native JavaScript Date.
It'd be a GWT-specific API but at least it won't be deprecated if
that's really bothering you.

kozura

unread,
Mar 19, 2010, 12:32:54 PM3/19/10
to Google Web Toolkit
And just for kicks, the JavaScript date class returns different values
for older dates than the Java class, and neither of them deal well
with older dates, time zones, and date manipulation anyway. But, for
normal applications using modern dates in a standard, simple way it is
adequate, and importantly lightweight. A true Calendar-type class set
requires a substantial amount of code that would have to be sent to
the client, bloating your app download size - just so you can get what
year it is.

In reality, the "deprecated" Date object is simply used as an
interface, as GWT just uses it to wrap the JS date class calls
anyway. So, besides the annoying warnings which can easily be turned
off, why write a new GWT specific API to do the same wrapping? Seems
it would be better to write a real correctly working Calendar class
for those few apps that actually need the advanced functionality.

For my app I've actually had to write very correct calendars that span
all of time (15BY, not 6000YA...), including functions like the roll
and getting the number of days in a given month/year like Java's
class. With Gregorian/Julian/Proleptic/Astronomic versions it turns
into about 10k of compiled js. Unfortunately it's probably too
detailed to push out as the open source general calendar solution for
GWT..

Reply all
Reply to author
Forward
0 new messages