当前位置:网站首页>时间戳的拓展及应用实例
时间戳的拓展及应用实例
2022-07-06 00:17:00 【王小小鸭】
拓展
近期学习遇到的一些应用实例可以用时间戳来解决,而获取时间戳有以下三种方式,推荐使用System类来获取时间戳,下面一起看一看三种方式:
1.System.currentTimeMillis()
System类中的currentTimeMilis()方法是三种方式中效率最好的,运行时间最短。开发中如果设计到效率问题,推荐使用此种方式获取。
2.new Date().getTime()
除了System类,使用量很大的应该就是Date类了,开发中如果涉及到日期的首先会想到Date,但date类中获取时间戳并不是最有效率的,翻看他的源码:
无参构造如下
public Date() {
this(System.currentTimeMillis());
从源码可以看出, new Date()其实就是调用了System.currentTimeMillis(),再传入自己的有参构造函数。不难看出,如果只是仅仅获取时间戳,即使是匿名的new Date()对象也会有些许的性能消耗,从提升性能的角度来看,只是仅仅获取时间戳,不考虑时区的影响(时区为什么会有影响看下一段),直接调用System.currentTimeMillis()会更好一些。
3.Calendar.getInstance().getTimelnMillis()
这种方式其实是速度最慢,看其源码就会发现,Canlendar是区分时区的,因为要处理时区问题会耗费很多的时间。
综上所述,建议使用System.currentTimeMillis()获取时间戳。
应用实例:
求两个日期之间相隔的天数
例如:写一个方法(fun3(“2010-09-20”,“2010-09-21”) ) 结果为1天
package com.com.object_11.pratice_11.A4;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CalendarDemo {
public static void main(String[] args) {
// 使用时间戳
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date star = dft.parse("2022-06-30");//开始时间
Date endDay=dft.parse("2025-11-30");//结束时间
Long starTime=star.getTime();
Long endTime=endDay.getTime();
Long num=endTime-starTime;//时间戳相差的毫秒数
System.out.println("相差天数为:"+num/24/60/60/1000);//除以一天的毫秒数
} catch (ParseException e) {
e.printStackTrace();
}
}
}

出生了多少天
请使用日期时间相关的API,计算出一个人已经出生了多少天。
package com.com.object_11.pratice_11.A5;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//请使用日期时间相关的API,计算出一个人已经出生了多少天。
public class Birthday {
public static void main(String[] args) {
// 使用时间戳
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date star = dft.parse("1999-11-08");//开始时间
Date endDay=dft.parse("2022-06-30");//结束时间
Long starTime=star.getTime();
Long endTime=endDay.getTime();
Long num=endTime-starTime;//时间戳相差的毫秒数
System.out.println("出生至今的天数为:"+num/24/60/60/1000);//除以一天的毫秒数
} catch (ParseException e) {
e.printStackTrace();
}
}
}

打印数字1-9999所需要使用的时间
验证for循环打印数字1-9999所需要使用的时间(毫秒)
package com.com.object_11.pratice_11.A6;
//验证for循环打印数字1-9999所需要使用的时间(毫秒)
public class PrintTimes {
public static void main(String[] args) {
// 开始时间
long start = System.currentTimeMillis();
for (int i = 1; i < 10000; i++) {
System.out.println(i);
}
// 结束时间
long end = System.currentTimeMillis();
//开始时间减去结束时间即为打印所需时间
System.out.println("打印1-9999需要" + (end - start) + "毫秒");
}
}

距离某天还有几天
求出今天距离2023年1月1日还有多少天
package com.com.object_11.pratice_11.A7;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FutureTimes {
public static void main(String[] args) throws ParseException {
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
Date endDay=dft.parse("2023-01-01");//结束时间
// 使用时间戳
Long starTime=System.currentTimeMillis();//开始时间(当前)
Long endTime=endDay.getTime();
Long num=endTime-starTime;//时间戳相差的毫秒数
System.out.println("今天距离2023年1月1日还有:"+num/24/60/60/1000+"天");//除以一天的毫秒数
}
}
//今天距离2023年1月1日还有:184天

边栏推荐
猜你喜欢

时区的区别及go语言的time库

Determinant learning notes (I)
![Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]

wx. Getlocation (object object) application method, latest version

Key structure of ffmpeg - avframe

【DesignMode】组合模式(composite mode)

Qt QPushButton详解

【二叉搜索树】增删改查功能代码实现

FFMPEG关键结构体——AVFrame

硬件及接口学习总结
随机推荐
7.5 装饰器
MySql——CRUD
【DesignMode】适配器模式(adapter pattern)
Ffmpeg learning - core module
【QT】Qt使用QJson生成json文件并保存
LeetCode 6006. Take out the least number of magic beans
QT QPushButton details
Codeforces round 804 (Div. 2) [competition record]
DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
【luogu P3295】萌萌哒(并查集)(倍增)
Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
Shardingsphere source code analysis
认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
MySQL之函数
DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
Doppler effect (Doppler shift)
PHP determines whether an array contains the value of another array
Priority queue (heap)
剖面测量之提取剖面数据