Aug 03

All the constructors and methods of the Date class that allow you to set the date value are deprecated so how do we set a Date instance to the date we need it to be.

Answer is to use the Calendar class and use the various set() methods to set the date required. Once you have that set you can use the getTime() method to get your required Date.

Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
Date date = cal.getTime();

An alternative is to use the GregorianCalendar class directly

Calendar cal = new GregorianCalendar(year, month, day);
Date date = cal.getTime();

written by objects \\ tags: , ,

Apr 01

The TimeZone class has a getOffset() method that will return the offset for a specified date. For example to get the timezone offset for today you would use the following:

Date today = new Date();
TimeZone tz = TimeZone.getDefault();
long offset = tz.getOffset(today);

written by objects \\ tags: , ,

Oct 13

The Calendar class can be used to do a variety of date arithmetic


Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, numberOfHours);
date = cal.getTime(); 

written by objects \\ tags: ,