Home >Java >javaTutorial >JAVA instance method to obtain current system events

JAVA instance method to obtain current system events

PHP中文网
PHP中文网Original
2017-06-20 16:20:501353browse

System.currentTimeMillis() generates a current millisecond, which is actually the number of milliseconds since 0:00 on January 1, 1970. Date() is actually equivalent to Date(System.currentTimeMillis()), because Date The class also has a constructor Date(longdate), which is used to calculate the millisecond difference between long seconds and January 1, 1970.

After getting this number of milliseconds, we can also calculate the current year, month, day, and week by ourselves, but this is not calculated by us, because there is Calendar. The final result of Calendar is the year, month, day, week, and hour Time zone.

System.currentTimeMillis() obtains the time distance from 1970-01-01 00:00:00.000 to the current moment, the type is long

String.valueOf(System.currentTimeMillis() ) This statement can be converted into the following format:

long ct = System.currentTimeMillis();

String t = String.valueOf(ct);

In fact, the above String t is equivalent to ct+"";

Convert knowledge to string format

public String refFormatNowDate(){

Date nowTime = new Date(System.currentTimeMillis() );

SimpleDateFormat sdFormatter = new SimpleDateFormat("yyy-MM-dd");

String retStrFormatNowDate = sdFormatter.format(nowTime);

return retStrFormatNowDate;

}

The above is the detailed content of JAVA instance method to obtain current system events. 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