当前位置:网站首页>时间戳的拓展及应用实例
时间戳的拓展及应用实例
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天
边栏推荐
- 上门预约服务类的App功能详解
- Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
- [designmode] Decorator Pattern
- mysql-全局锁和表锁
- Go learning --- read INI file
- QT QPushButton details
- 硬件及接口学习总结
- Transport layer protocol ----- UDP protocol
- About the slmgr command
- Search (DFS and BFS)
猜你喜欢
LeetCode 1598. Folder operation log collector
openssl-1.0.2k版本升级openssl-1.1.1p
How to solve the problems caused by the import process of ecology9.0
Gd32f4xx UIP protocol stack migration record
FFMPEG关键结构体——AVFrame
wx.getLocation(Object object)申请方法,最新版
MySql——CRUD
Configuring OSPF load sharing for Huawei devices
NSSA area where OSPF is configured for Huawei equipment
What are Yunna's fixed asset management systems?
随机推荐
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
[day39 literature extensive reading] a Bayesian perspective on magnetic estimation
Atcoder beginer contest 258 [competition record]
Open3D 点云随机添加噪声
Single merchant v4.4 has the same original intention and strength!
[Luogu p3295] mengmengda (parallel search) (double)
Huawei equipment configuration ospf-bgp linkage
提升工作效率工具:SQL批量生成工具思想
shardingsphere源码解析
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
SQLServer连接数据库读取中文乱码问题解决
Tools to improve work efficiency: the idea of SQL batch generation tools
FFMPEG关键结构体——AVFrame
认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
转:未来,这样的组织才能扛住风险
Determinant learning notes (I)
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
Key structure of ffmpeg - avframe
微信小程序---WXML 模板语法(附带笔记文档)
Search (DFS and BFS)