UNIX timestamp of each language

Source: Internet
Author: User
Tags time zones posix

Concept:

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.
That is:
Long epoch = system. currenttimemillis ()/1000;

Method:

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 ();

Note:

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 ());
Output:
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 );
Output:
Zone 8 current time: 21:13:47

 

 

 

 

 

========================================================== ====================

 

 

What is a UNIX timestamp? (Reprinted) What is a UNIX timestamp ( UNIX Timestamp)?

UNIX timestamp, or Unix time or POSIX time, is a time representation, it is defined as the total number of seconds from GMT, January 1, January 01, 1970 to the present. UNIX timestamps are used not only in UNIX and Unix-like systems, but also in many other operating systems.

Currently, a considerable number of operating systems use 32-bit binary numbers to represent the time. The UNIX timestamp of such systems can be used up to GMT, January 1, January 19, 2038 (Binary:01111111 11111111 11111111 11111111). In the next second, the binary number will change10000000 00000000 00000000 00000000An overflow error occurs, causing the system to misunderstand the time as December 13, 1901. This may cause software faults or even system paralysis. Systems that use 64-bit binary numbers to represent time (a maximum of 292,277,026,596 seconds from January 1, December 04 to GMT) will not experience such overflow issues.

How to obtain the current UNIX timestamp in different programming languages ( UNIX Timestamp)?
Java Time
Javascript Math. Round (new date (). gettime ()/1000)
The unit of the value returned by gettime () is millisecond.
Microsoft. NET/C # Epoch = (datetime. Now. touniversaltime (). ticks-621355968000000000)/10000000
MySQL Select unix_timestamp (now ())
Perl Time
PHP Time ()
PostgreSQL Select extract (epoch from now ())
Python Import time first and then time. Time ()
Ruby Obtain UNIX timestamp: time. Now or time. New
Display UNIX timestamp: time. Now. to_ I
SQL Server Select datediff (S, '2017-01-01 00:00:00 ', getutcdate ())
Unix/Linux Date + % s
VBScript/asp Datediff ("S", "01/01/1970 00:00:00", now ())
Other operating systems
(If Perl is installed in the System)
Command Line status: Perl-e "Print time"
How to Implement UNIX timestamps in different programming languages ( UNIX Timestamp) → Normal time?
Java String date = new java. Text. simpledateformat ("DD/MM/YYYY hh: mm: SS"). Format (New java. util. Date (UNIX timestamp * 1000 ))
Javascript First var unixtimestamp = new date (UNIX timestamp * 1000) and then commontime = unixtimestamp. tolocalestring ()
Linux Date-d @ UNIX Timestamp
MySQL From_unixtime (UNIX timestamp)
Perl First my $ time = UNIX Timestamp and then my ($ sec, $ min, $ hour, $ day, $ month, $ year) = (localtime ($ time) [0, 1, 2, 3, 4, 5, 6]
PHP Date ('R', Unix timestamp)
PostgreSQL Select timestamp with Time Zone 'epoch' + UNIX timestamp) * interval '1 second ';
Python Import time first and then time. gmtime (UNIX timestamp)
Ruby Time. At (UNIX timestamp)
SQL Server Dateadd (S, Unix timestamp, '2017-01-01 00:00:00 ')
VBScript/asp Dateadd ("S", Unix timestamp, "01/01/1970 00:00:00 ")
Other operating systems
(If Perl is installed in the System)
Command Line status: Perl-e "Print scalar (localtime (UNIX timestamp ))"
How to implement common time → UNIX timestamp in different programming languages ( UNIX Timestamp)?
Java Long epoch = new java. Text. simpledateformat ("DD/MM/YYYY hh: mm: SS"). parse ("01/01/1970 01:00:00 ");
Javascript VaR commontime = new date (date. UTC (year, month-1, day, hour, minute, second ))
MySQL Select unix_timestamp (time)
Time Format: YYYY-MM-DD hh: mm: SS or yymmdd or yyyymmdd
Perl Use Time: local first and then my $ time = timelocal ($ sec, $ min, $ hour, $ day, $ month, $ year );
PHP Mktime (hour, minute, second, day, month, year)
PostgreSQL Select extract (epoch from date ('yyyy-MM-DD hh: mm: ss '));
Python First import time and then int (time. mktime (time. strptime ('yyyy-MM-DD hh: mm: ss', '% Y-% m-% d % H: % m: % s ')))
Ruby Time. Local (year, month, day, hour, minute, second)
SQL Server Select datediff (S, '2017-01-01 00:00:00 ', time)
Unix/Linux Date + % s-d "Jan 1, 1970 00:00:01"
VBScript/asp Datediff ("S", "01/01/1970 00:00:00", time)
What is a UNIX timestamp ( UNIX Timestamp)?

UNIX timestamp, or Unix time or POSIX time, is a time representation, it is defined as the total number of seconds from GMT, January 1, January 01, 1970 to the present. UNIX timestamps are used not only in UNIX and Unix-like systems, but also in many other operating systems.

Currently, a considerable number of operating systems use 32-bit binary numbers to represent the time. The UNIX timestamp of such systems can be used up to GMT, January 1, January 19, 2038 (Binary:01111111 11111111 11111111 11111111). In the next second, the binary number will change10000000 00000000 00000000 00000000An overflow error occurs, causing the system to misunderstand the time as December 13, 1901. This may cause software faults or even system paralysis. Systems that use 64-bit binary numbers to represent time (a maximum of 292,277,026,596 seconds from January 1, December 04 to GMT) will not experience such overflow issues.

How to obtain the current UNIX timestamp in different programming languages ( UNIX Timestamp)?
Java Time
Javascript Math. Round (new date (). gettime ()/1000)
The unit of the value returned by gettime () is millisecond.
Microsoft. NET/C # Epoch = (datetime. Now. touniversaltime (). ticks-621355968000000000)/10000000
MySQL Select unix_timestamp (now ())
Perl Time
PHP Time ()
PostgreSQL Select extract (epoch from now ())
Python Import time first and then time. Time ()
Ruby Obtain UNIX timestamp: time. Now or time. New
Display UNIX timestamp: time. Now. to_ I
SQL Server Select datediff (S, '2017-01-01 00:00:00 ', getutcdate ())
Unix/Linux Date + % s
VBScript/asp Datediff ("S", "01/01/1970 00:00:00", now ())
Other operating systems
(If Perl is installed in the System)
Command Line status: Perl-e "Print time"
How to Implement UNIX timestamps in different programming languages ( UNIX Timestamp) → Normal time?
Java String date = new java. Text. simpledateformat ("DD/MM/YYYY hh: mm: SS"). Format (New java. util. Date (UNIX timestamp * 1000 ))
Javascript First var unixtimestamp = new date (UNIX timestamp * 1000) and then commontime = unixtimestamp. tolocalestring ()
Linux Date-d @ UNIX Timestamp
MySQL From_unixtime (UNIX timestamp)
Perl First my $ time = UNIX Timestamp and then my ($ sec, $ min, $ hour, $ day, $ month, $ year) = (localtime ($ time) [0, 1, 2, 3, 4, 5, 6]
PHP Date ('R', Unix timestamp)
PostgreSQL Select timestamp with Time Zone 'epoch' + UNIX timestamp) * interval '1 second ';
Python Import time first and then time. gmtime (UNIX timestamp)
Ruby Time. At (UNIX timestamp)
SQL Server Dateadd (S, Unix timestamp, '2017-01-01 00:00:00 ')
VBScript/asp Dateadd ("S", Unix timestamp, "01/01/1970 00:00:00 ")
Other operating systems
(If Perl is installed in the System)
Command Line status: Perl-e "Print scalar (localtime (UNIX timestamp ))"
How to implement common time → UNIX timestamp in different programming languages ( UNIX Timestamp)?
Java Long epoch = new java. Text. simpledateformat ("DD/MM/YYYY hh: mm: SS"). parse ("01/01/1970 01:00:00 ");
Javascript VaR commontime = new date (date. UTC (year, month-1, day, hour, minute, second ))
MySQL Select unix_timestamp (time)
Time Format: YYYY-MM-DD hh: mm: SS or yymmdd or yyyymmdd
Perl Use Time: local first and then my $ time = timelocal ($ sec, $ min, $ hour, $ day, $ month, $ year );
PHP Mktime (hour, minute, second, day, month, year)
PostgreSQL Select extract (epoch from date ('yyyy-MM-DD hh: mm: ss '));
Python First import time and then int (time. mktime (time. strptime ('yyyy-MM-DD hh: mm: ss', '% Y-% m-% d % H: % m: % s ')))
Ruby Time. Local (year, month, day, hour, minute, second)
SQL Server Select datediff (S, '2017-01-01 00:00:00 ', time)
Unix/Linux Date + % s-d "Jan 1, 1970 00:00:01"
VBScript/asp Datediff ("S", "01/01/1970 00:00:00", time)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.