API
记录Java工程中用于获取当前时间的API方法
System.currentTimeMillis()
- 作用:返回自1970年1月1日00:00:00 GMT以来的毫秒数。
- 返回值类型:
long
。 - 使用场景:主要用于时间间隔计算、性能监测和时间戳记录等场景。
示例:
long currentTimeMillis = System.currentTimeMillis();
System.out.println("Current Time in Milliseconds: " + currentTimeMillis);
new Date()
- 作用:创建一个表示当前日期和时间的
Date
对象。 - 返回值类型:
java.util.Date
。 - 使用场景:用于表示具体的时间点,可以与其他日期和时间类进行相互转换和操作。
示例:
Date currentDate = new Date();
System.out.println("Current Date: " + currentDate);
System.currentTimeMillis()
主要用于获取时间戳和计算时间间隔。new Date()
主要用于生成具体的日期和时间对象,以便进一步操作。