How to get an hour ago in Java

This example shows you how to get an hour ago in Java using java.util.Calendar.

DateTimeExample.java
package com.bytenota;

import java.util.Calendar;
import java.util.Date;

public class DateTimeExample {
    
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, - 1);
        Date lastHour = cal.getTime();
        
        System.out.println(lastHour.toString());
        System.out.println(lastHour.getTime()); // timestamp in milliseconds
        System.out.println(lastHour.getTime() / 1000L); // timestamp in seconds
    }
}

Output:

Thu Nov 30 10:36:22 ICT 2017
1512012982728
1512012982

0 0 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x