Date date = new Date();
int lastMonth = date.getMonth();
int lastYear = 1900 + date.getYear();
I have this code in my GWT project but a lot of method of java.util.Date is deprecated, 22/33 methods.
So I want to use Calendar/GregorianCalendar : it does not work. It said there is a missing package.
What is the best practice to replace these line of code with a not deprecated code ?
Don’t.
java.util.Calendaris almost impossible to emulate, and has a way too high overhead.java.util.Date, while far from ideal, is the closest to JavaScript’sDate, so it’s the one you should use in GWT world.JSR 310 is promising (don’t know how well it could be emulated though, and with how much overhead compared to
java.util.Date) but isn’t there yet.There’s also Joda Time (from the same author as JSR 310, JSR 310 aims at bringing Joda Time to the JDK along with fixing some of its early design mistakes), but most GWT ports have long been abandonned.
In the end: use
java.util.Date.And if you find the deprecation warnings annoying, then suppress them:
@SuppressWarnings("deprecated").