Wednesday, July 30, 2008

Java: Date manipulation

The method below that gets current date, returns the next exact hour (i.e. 12:00:00:00).
Notice ".add" method.

public static Date getNextHour(Date date)

   

{

Calendar cal = new GregorianCalendar();

  cal.setTime(date);

  cal.add(Calendar.HOUR, 1);

  cal.set(Calendar.MINUTE, 0);

  cal.set(Calendar.SECOND, 0);

  cal.set(Calendar.MILLISECOND, 0);

  Date time = cal.getTime();

 

  return time;

    }