当前位置:网站首页>时间戳的拓展及应用实例
时间戳的拓展及应用实例
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天
边栏推荐
- 跟着CTF-wiki学pwn——ret2libc1
- 2022.7.5-----leetcode. seven hundred and twenty-nine
- Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
- 18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
- [Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
- 云呐|固定资产管理系统主要操作流程有哪些
- FFmpeg学习——核心模块
- openssl-1.0.2k版本升级openssl-1.1.1p
- 行列式学习笔记(一)
- FFMPEG关键结构体——AVFrame
猜你喜欢
【二叉搜索树】增删改查功能代码实现
电机的简介
XML configuration file (DTD detailed explanation)
Transport layer protocol ----- UDP protocol
China Jinmao online electronic signature, accelerating the digitization of real estate business
How much do you know about the bank deposit business that software test engineers must know?
Ffmpeg learning - core module
【DesignMode】组合模式(composite mode)
Classical concurrency problem: the dining problem of philosophers
Senparc. Weixin. Sample. MP source code analysis
随机推荐
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
How to solve the problems caused by the import process of ecology9.0
What are Yunna's fixed asset management systems?
Qt 一个简单的word文档编辑器
USB Interface USB protocol
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Search (DFS and BFS)
Key structure of ffmpeg -- AVCodecContext
上门预约服务类的App功能详解
Chapter 16 oauth2authorizationrequestredirectwebfilter source code analysis
Asynchronous task Whenall timeout - Async task WhenAll with timeout
【luogu P3295】萌萌哒(并查集)(倍增)
Miaochai Weekly - 8
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
Priority queue (heap)
Atcoder beginer contest 258 [competition record]
Atcoder beginer contest 254 [VP record]
"14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
Ffmpeg learning - core module