Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to get the current timestamp

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you how to use Java to get the current timestamp. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.

To get the current timestamp in Java:

Timestamp timestamp = new Timestamp (System.currentTimeMillis ()); / / 2016-11-1606 purl 4319.77

These are two Java examples that show you how to get the current timestamp in Java. (updated with Java 8)

1. Java.sql.Timestamp

Two ways to get the current java.sql.Timestamp

TimeStampExample.java

Package com.mkyong.date; import java.sql.Timestimport java.text.SimpleDateFormat;import java.util.Date;public class TimeStampExample {private static final SimpleDateFormat sdf = new SimpleDateFormat ("yyyy.MM.dd.HH.mm.ss"); public static void main (String [] args) {/ / method 1 Timestamp timestamp = new Timestamp (System.currentTimeMillis ()); System.out.println (timestamp); / / method 2-via Date Date date = new Date () System.out.println (new Timestamp (date.getTime)); / / return number of milliseconds since January 1, 1970, 00:00:00 GMT System.out.println (timestamp.getTime ()); / / format timestamp System.out.println (sdf.format (timestamp));}}

Output quantity

2016-11-1606 purl 4319.77

2016-11-16 06Performing 4319.769

1479249799770

2016.11.16.06.43.19

2. Java.time.Instant

In Java 8, you can convert java.sql.Timestamp to a new java.time.Instant

InstantExample.java

Package com.mkyong.date; import java.sql.Timestimport java.time.Instant;public class InstantExample {public static void main (String [] args) {Timestamp timestamp = new Timestamp (System.currentTimeMillis ()); System.out.println (timestamp); / / return number of milliseconds since January 1, 1970, 00:00:00 GMT System.out.println (timestamp.getTime ()); / / Convert timestamp to instant Instant instant = timestamp.toInstant () System.out.println (instant); / / return number of milliseconds since the epoch of 1970-01-01T00:00:00Z System.out.println (instant.toEpochMilli ()); / / Convert instant to timestamp Timestamp tsFromInstant = Timestamp.from (instant); System.out.println (tsFromInstant.getTime ());}}

Output quantity

2016-11-16 06Rose 5515

1479250540110

2016-11-15T22:55:40.110Z

1479250540110

1479250540110

Add: the method of getting the current timestamp by java

Get the current timestamp

/ / method 1 System.currentTimeMillis (); / / method 2 Calendar.getInstance () .getTimeInMillis (); / / method 3 new Date () .getTime ()

Get the current time

SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); / / set the date format String date = df.format (new Date ()); / / new Date () to get the current system time, you can also use the current timestamp

Comparison of the execution efficiency of the three methods for obtaining timestamps:

Import java.util.Calendar;import java.util.Date; public class TimeTest {private static long _ TEN_THOUSAND=10000; public static void main (String [] args) {long times=1000*_TEN_THOUSAND; long t1=System.currentTimeMillis (); testSystem (times); long t2=System.currentTimeMillis (); System.out.println (t2-t1); testCalander (times); long t3=System.currentTimeMillis () System.out.println (t3-t2); testDate (times); long t4=System.currentTimeMillis (); System.out.println (t4-t3);} public static void testSystem (long times) {/ / use 188for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report