当前位置:网站首页>Extension and application of timestamp
Extension and application of timestamp
2022-07-06 00:22:00 【Wang Xiaoya】
expand
Some application examples encountered in recent learning can be solved by timestamp , There are three ways to obtain timestamp , Recommended System Class to get the timestamp , Let's take a look at three ways :
1.System.currentTimeMillis()
System Class currentTimeMilis() The method is the most efficient of the three , The shortest running time . In the development, if the design comes to the problem of efficiency , It is recommended to use this method to obtain .
2.new Date().getTime()
except System class , The ones that use a lot should be Date The class , If the date is involved in the development, you will first think of Date, but date Getting a timestamp in a class is not the most efficient , Look at his source code :
The parameterless structure is as follows
public Date() {
this(System.currentTimeMillis());
You can see from the source code , new Date() It's actually called System.currentTimeMillis(), Then pass in your own parameter constructor . It's not hard to see. , If you just get the timestamp , Even anonymous new Date() Objects also have a slight performance cost , From the perspective of improving performance , Just get the timestamp , Regardless of the time zone ( Why does time zone have an impact? Let's see the next paragraph ), Call directly System.currentTimeMillis() It will be better .
3.Calendar.getInstance().getTimelnMillis()
This method is actually the slowest , Look at its source code and you will find ,Canlendar It's time zone sensitive , Because it takes a lot of time to deal with the time zone problem .
in summary , It is recommended to use System.currentTimeMillis() Get the timestamp .
Application example :
Find the number of days between two dates
for example : Write a method (fun3(“2010-09-20”,“2010-09-21”) ) The result is 1 God
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) {
// Use time stamps
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date star = dft.parse("2022-06-30");// Starting time
Date endDay=dft.parse("2025-11-30");// End time
Long starTime=star.getTime();
Long endTime=endDay.getTime();
Long num=endTime-starTime;// The number of milliseconds between timestamps
System.out.println(" The difference in days is :"+num/24/60/60/1000);// Divide by the number of milliseconds in a day
} catch (ParseException e) {
e.printStackTrace();
}
}
}

How many days were you born
Please use date time related API, Calculate how many days a person has been born .
package com.com.object_11.pratice_11.A5;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
// Please use date time related API, Calculate how many days a person has been born .
public class Birthday {
public static void main(String[] args) {
// Use time stamps
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
try {
Date star = dft.parse("1999-11-08");// Starting time
Date endDay=dft.parse("2022-06-30");// End time
Long starTime=star.getTime();
Long endTime=endDay.getTime();
Long num=endTime-starTime;// The number of milliseconds between timestamps
System.out.println(" The number of days since birth is :"+num/24/60/60/1000);// Divide by the number of milliseconds in a day
} catch (ParseException e) {
e.printStackTrace();
}
}
}

Print digit 1-9999 The time needed
verification for Cycle numbers 1-9999 The time needed ( millisecond )
package com.com.object_11.pratice_11.A6;
// verification for Cycle numbers 1-9999 The time needed ( millisecond )
public class PrintTimes {
public static void main(String[] args) {
// Starting time
long start = System.currentTimeMillis();
for (int i = 1; i < 10000; i++) {
System.out.println(i);
}
// End time
long end = System.currentTimeMillis();
// The start time minus the end time is the time required for printing
System.out.println(" Print 1-9999 need " + (end - start) + " millisecond ");
}
}

A few days away
So let's figure out today's distance 2023 year 1 month 1 How many days are there in the day
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");// End time
// Use time stamps
Long starTime=System.currentTimeMillis();// Starting time ( At present )
Long endTime=endDay.getTime();
Long num=endTime-starTime;// The number of milliseconds between timestamps
System.out.println(" Today's distance 2023 year 1 month 1 Day also :"+num/24/60/60/1000+" God ");// Divide by the number of milliseconds in a day
}
}
// Today's distance 2023 year 1 month 1 Day also :184 God

边栏推荐
- The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
- LeetCode 斐波那契序列
- 多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
- Classical concurrency problem: the dining problem of philosophers
- [noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
- Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
- QT -- thread
- LeetCode 1598. Folder operation log collector
- MySQL functions
- Yunna | what are the main operating processes of the fixed assets management system
猜你喜欢

Gd32f4xx UIP protocol stack migration record

Data analysis thinking analysis methods and business knowledge - analysis methods (III)

提升工作效率工具:SQL批量生成工具思想

Detailed explanation of APP functions of door-to-door appointment service

MySQL functions
![Go learning --- structure to map[string]interface{}](/img/e3/59caa3f2ba5bd3647bdbba075ee60d.jpg)
Go learning --- structure to map[string]interface{}

小程序技术优势与产业互联网相结合的分析

Configuring OSPF load sharing for Huawei devices

云呐|公司固定资产管理系统有哪些?

FFMPEG关键结构体——AVCodecContext
随机推荐
Determinant learning notes (I)
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
MySQL functions
2022.7.5-----leetcode. seven hundred and twenty-nine
Hudi of data Lake (2): Hudi compilation
How much do you know about the bank deposit business that software test engineers must know?
Basic introduction and source code analysis of webrtc threads
There is no network after configuring the agent by capturing packets with Fiddler mobile phones
如何解决ecology9.0执行导入流程流程产生的问题
[designmode] Decorator Pattern
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
STM32 configuration after chip replacement and possible errors
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
Uniapp development, packaged as H5 and deployed to the server
Transport layer protocol ----- UDP protocol
Yolov5、Pycharm、Anaconda环境安装
权限问题:source .bash_profile permission denied
LeetCode 斐波那契序列
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode:20220213 week race (less bugs, top 10% 555)