当前位置:网站首页>时间戳的拓展及应用实例
时间戳的拓展及应用实例
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天
边栏推荐
- 亲测可用fiddler手机抓包配置代理后没有网络
- Mathematical model Lotka Volterra
- 微信小程序---WXML 模板语法(附带笔记文档)
- How much do you know about the bank deposit business that software test engineers must know?
- 7.5模拟赛总结
- Configuring OSPF GR features for Huawei devices
- China Jinmao online electronic signature, accelerating the digitization of real estate business
- Hudi of data Lake (2): Hudi compilation
- 2022.7.5-----leetcode.729
- Asynchronous task Whenall timeout - Async task WhenAll with timeout
猜你喜欢
Tools to improve work efficiency: the idea of SQL batch generation tools
Key structure of ffmpeg - avformatcontext
Doppler effect (Doppler shift)
多普勒效應(多普勒頻移)
硬件及接口学习总结
wx.getLocation(Object object)申请方法,最新版
18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
Huawei equipment is configured with OSPF and BFD linkage
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
随机推荐
Configuring OSPF GR features for Huawei devices
【二叉搜索树】增删改查功能代码实现
亲测可用fiddler手机抓包配置代理后没有网络
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
[day39 literature extensive reading] a Bayesian perspective on magnetic estimation
About the slmgr command
FFT 学习笔记(自认为详细)
What are the functions of Yunna fixed assets management system?
Mysql - CRUD
如何解决ecology9.0执行导入流程流程产生的问题
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
LeetCode 6006. Take out the least number of magic beans
18. (ArcGIS API for JS) ArcGIS API for JS point collection (sketchviewmodel)
[Luogu p3295] mengmengda (parallel search) (double)
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
Chapter 16 oauth2authorizationrequestredirectwebfilter source code analysis
China Jinmao online electronic signature, accelerating the digitization of real estate business
【luogu CF487E】Tourists(圆方树)(树链剖分)(线段树)
LeetCode 1189. Maximum number of "balloons"