get current time in java ten minutes ago

You can use the java.util.Calendar class to get the current time, and use the getTime() method to obtain a Date object. Then, you can use the getTime() method of the java.util.Date class to get the milliseconds of the current time. Finally, subtract ten minutes worth of milliseconds (600,000 milliseconds) from the current time’s milliseconds, and use the setTime() method of the Date class to set the result as the new time value.

Here is an example code:

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

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        Date currentTime = calendar.getTime();

        long currentMilliseconds = currentTime.getTime();
        long tenMinutesMilliseconds = 10 * 60 * 1000;
        long tenMinutesAgoMilliseconds = currentMilliseconds - tenMinutesMilliseconds;

        Date tenMinutesAgo = new Date();
        tenMinutesAgo.setTime(tenMinutesAgoMilliseconds);

        System.out.println("当前时间:" + currentTime);
        System.out.println("当前时间前十分钟:" + tenMinutesAgo);
    }
}

This code will display the current time and the time ten minutes before the current time.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds