How to Subtract Hours in Java


In this example we will show the method to subtract hours in Java.

Source Code

package com.beginner.examples;

import java.util.Calendar;

public class SubtractHours {
  public static void main(String[] args){
    Calendar c = Calendar.getInstance();
    System.out.println(c.get(Calendar.HOUR));
    //hours add 1
    c.add(Calendar.HOUR, -1);
    System.out.println(c.get(Calendar.HOUR));
  }
}

Output:

9
8

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments