UNIX timestamp: Unix timestamp (UNIX epoch, Unix time, POSIX time, or Unix timestamp)
It is the number of seconds that have elapsed since January 1, January 1, 1970 (midnight UTC/GMT), regardless of the leap second.
The UNIX timestamp 0 follows the ISO 8601 standard: 1970-01-01t00: 00: 00Z.
The UNIX timestamp format for one hour is 3600 seconds. The UNIX timestamp for one day is 86400 seconds. The leap second is not calculated.
In most Unix systems, the Unix timestamp is stored as 32-bit, which may cause the 2038 issue or y2038.
1 minute 60 seconds
1 hour 3600 seconds
1 day, 86400 seconds
1 week, 604800 seconds
January 1, January (30.44 days), 2629743 seconds
1 year (365.24 days) 31556926 seconds
Why is UNIX timestamp used?
In today's systems, cross-database application development is often encountered. Different databases in the database system have different interpretations of different time types, for example, Oracle date and MySQL date cannot be directly compatible with conversion, and data can also be converted using data migration tools, however, for applications, it is a disaster (not to mention Hibernate and other frameworks that can break down databases ).
In order to record time in the application system, we can use the Unix timestamp record method to break down the platform.
Currently, most languages such as Java, PHP, and Perl support taking the Unix timestamp directly and record the time to be recorded as the Unix timestamp, in this way, we can break down the platform in different database systems, and only need to perform timestamp operations on and time. In fact, this processing method can be seen in many open-source systems, but it is not a habit for people who often engage in J2EE. Fortunately, I have a PHP base.
The following describes how to handle the problem:
==================================== Obtain the Unix timestamp of the system ====== ==========================================
PHP time ()
Ruby time. Now (or time. New). To display the epoch: time. Now. to_ I
Python import time first, then time. Time ()
Java long epoch = system. currenttimemillis ()/1000;
Microsoft. net c # epoch = (datetime. Now. touniversaltime (). ticks-621355968000000000)/10000000;
VBScript/asp datediff ("S", "01/01/1970 00:00:00", now ())
MySQL select unix_timestamp (now ())
PostgreSQL select extract (epoch from now ());
SQL Server select datediff (S, '20170101', getdate ())
Unix/Linux date + % s
Other OS's command line: Perl-e "Print time" (if Perl is installed on your system)
================================================== Convert time to Unix timestamp = ==============================================
Perl use these Perl epoch routines
PHP mktime (hour, minute, second, month, day, year)
Ruby time. Local (year, month, day, hour, minute, second, USEC) (or time. GM for GMT/UTC input). To display Add. to_ I
Python import time first, then int (time. mktime (time. strptime ('2017-01-01 12:34:00 ',' % Y-% m-% d % H: % m: % s ')))
Java long epoch = new Java. Text. simpledateformat ("DD/MM/YYYY hh: mm: SS"). parse ("01/01/1970 01:00:00 ");
VBScript/asp datediff ("S", "01/01/1970 00:00:00", time field)
MySQL select unix_timestamp (time) Time Format: YYYY-MM-DD hh: mm: SS or yymmdd or yyyymmdd
PostgreSQL select extract (epoch from date ('2017-01-01 '));
With timestamp: Select extract (epoch from timestamp with Time Zone '2017-02-16 20:38:40-08 ');
With interval: Select extract (epoch from interval '5 days 3 hours ');
SQL Server select datediff (S, '20170101', time field)
Unix/Linux date + % s-d "Jan 1, 1980 00:00:01"
================================================ Convert UNIX timestamps to time = ==============================================
Perl use these Perl epoch routines
PHP date (output format, EPOCH); output format example: 'R' = RFC 2822 date
Ruby time. At (EPOCH)
Python import time first, then time. gmtime (EPOCH) Time is an array of year, month, day, hour, Min, SEC, day of week, day of year, DST
Java string date = new Java. Text. simpledateformat ("DD/MM/YYYY hh: mm: SS"). Format (New java. util. Date (EPOCH * 1000 ));
VBScript/asp dateadd ("S", epoch, "01/01/1970 00:00:00 ")
PostgreSQL select timestamp with Time Zone 'epoch' + epoch * interval '1 second ';
MySQL from_unixtime (epoch, optional output format) the default output format is YYY-MM-DD hh: mm: SS
SQL Server dateadd (S, epoch, '20140901 ')
Linux date-d @ 1190000000 (replace 1190000000 with your epoch, needs newer version of date)
Other OS's command line: Perl-e "Print scalar (localtime (EPOCH)" (if Perl is installed) replace 'localtime' with 'gmtime' for GMT/UTC time.
UNIX timestamp application-Java
Non-special description, reference
System. currenttimemillis (): returns the number of milliseconds of the current system. Because the number of milliseconds is obtained, the Unix timestamp must be converted to seconds.
Long epoch = system. currenttimemillis ()/1000;
1. Obtain the Unix timestamp of the current system.
System. Out. println ("method for obtaining system milliseconds 1:" + long. tostring (new date (). gettime ()));
System. Out. println ("method for obtaining system milliseconds 2:" + long. tostring (system. currenttimemillis ()));
Note: The above code obtains the number of milliseconds in the system. In actual operations, we generally record the number of milliseconds for record precision. When processing UNIX timestamps, we need to process the data.
2. convert a Unix timestamp to a time that can be processed by the system.
System. out. println ("" + new Java. text. simpledateformat ("YYYY mm-dd hh: mm: SS "). format (New Java. util. date (1215782027390l )));
Output: 2008 21:13:47-11
Note: the processed data is in milliseconds rather than UNIX timestamp.
3. Convert Lecture time to Unix Timestamp
Long epoch = new java. Text. simpledateformat ("DD/MM/YYYY hh: mm: SS"). parse ("16:33:00"). gettime ();
Please note! When dealing with different time zones, you must first know your own time zone.
String timezone_info = system. getproperty ("user. timezone ");
System. Out. println ("Current Time Zone:" + timezone_info );
System. Out. println ("Time Zone Information:" + timezone. getdefault ());
Current Time Zone: Asia/Shanghai
Time zone information: Sun. util. Calendar. zoneinfo [ID = "Asia/Shanghai", offset = 28800000, dstsavings = 0, usedaylight = false, transitions = 19, lastrule = NULL]
How to deal with different time zones:
Simpledateformat SD = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
SD. settimezone (timezone. gettimezone ("GMT + 8 "));
String strdate = SD. Format (new date (1215782027390l ));
System. Out. println ("Zone 8 current time:" + strdate );
Zone 8 current time: 21:13:47