UNIX timestamp application-Java

Source: Internet
Author: User
Tags time zones iso 8601 time in milliseconds

Concept:
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.
Time seconds
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

Thoughts:
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.

Processing:

The following describes how to handle the problem:
==================================== Obtain the Unix timestamp of the system ====== ==========================================
Perl time
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 ())
Javascript math. Round (new date (). gettime ()/1000.0) gettime () returns time in milliseconds.
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)
Javascript use the Javascript date object
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 ')
Javascript use the Javascript date object
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
Xzeus.org thendmx@gmail.com
==================================

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

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.