Home  >  Article  >  Java  >  How to get the current system time in java

How to get the current system time in java

coldplay.xixi
coldplay.xixiOriginal
2020-12-18 11:27:586199browse

Java method to get the current time of the system: get the current time and the time 30 seconds before the current time, the code is [SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");].

How to get the current system time in java

The operating environment of this tutorial: Windows 7 system, Java 10 version. This method is suitable for all brands of computers.

How to get the current system time in java:

import org.junit.Test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
 * @author ceshi
 * @Title: ceshi
 * @ProjectName ceshi
 * @Description: TODO
 * @date 2018/7/59:48
 */
public class JunitCalendar {
    @Test
    public void test(){
        //获得当前时间和当前时间前30秒时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = new GregorianCalendar();
        Date date = new Date();
        System.out.println("系统当前时间"+df.format(date));
        //设置参数时间
        c.setTime(date);
        //SECOND 秒 整数增加,负数减少
        c.add(Calendar.SECOND,-10);
        System.out.println("系统前10秒时间:"+df.format(c.getTime()));
        //DAY_OF_MONTH 天 整数增加,负数减少
        c.add(Calendar.DAY_OF_MONTH,1);
        System.out.println("系统前1天时间:"+df.format(c.getTime()));
    }
}

Related free learning recommendations: java basic tutorial

The above is the detailed content of How to get the current system time in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn