Subtract 4 hours from the time and print out the date and time in Java

Description

The following code shows how to subtract 4 hours from the time and print out the date and time.

Example


/* ww w.  j  av a  2  s  .c o  m*/
import java.text.DateFormat;
import java.util.Calendar;

public class Main {
  public static void main(String s[]) {
    Calendar cal = Calendar.getInstance();
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL,
        DateFormat.MEDIUM);

    System.out.println(df.format(cal.getTime()));

    cal.add(Calendar.HOUR, -4);
    System.out.println(df.format(cal.getTime()));
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone