Java Get UNIX Timestamp

Source: Internet
Author: User

The UNIX timestamp is the number of seconds that have elapsed since the start of the January 1, 1970 (Midnight of Utc/gmt), regardless of the leap seconds.

In most Unix systems, the UNIX timestamp is stored as 32 bits, which raises the 2038 issue.

However, because the requirement is a UNIX timestamp that requires an int type. That's how I designed it at first.

/** * Gets the current event unxi timestamp * @return */public static int Getunixtimestamp () {long Rest=system.currenttimemillis ()/1000l;return ( int) rest;}

New encapsulation of some methods. The following stability is much better.

1  Packagecom.xuanyuan.utils;2 3  Public classTimeutils {4     5       /**6 * Constant that contains the amount of milliseconds in a second7      */8     Static Final LongOne_second = 1000L;9 Ten     /** One * Converts milliseconds to seconds A      * @paramTimeinmillis -      * @returnThe equivalent time in seconds -      */ the      Public Static intTosecs (LongTimeinmillis) { -         //rounding the result to the ceiling, otherwise a -         //System.currenttimeinmillis that happens right before a new Element -         //instantiation'll be seen as ' later ' than the actual creation time +         return(int) Math.ceil (Double) Timeinmillis/one_second); -     } +  A     /** at * Converts seconds to milliseconds, with a precision of 1 second -      * @paramTimeinsecs the time in seconds -      * @returnThe equivalent time in milliseconds -      */ -      Public Static LongTomillis (inttimeinsecs) { -         returnTimeinsecs *One_second; in     } -  to     /** + * Converts a long seconds value to an int seconds value and takes into account overflow - * from the downcast by switching to Integer.max_value. the      * @paramseconds Long Value *      * @returnsame int value unless long > integer.max_value in which case Max_value is returned $      */Panax Notoginseng      Public Static intConverttimetoint (Longseconds) { -         if(Seconds >integer.max_value) { the             returnInteger.max_value; +}Else { A             return(int) seconds; the         } +     } -  $}

Java Get UNIX Timestamp

Related Article

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.