How to obtain the system time in Java

Source: Internet
Author: User
Tags date now date1

Sometimes there is often a gap of 8 hours between the current time and the current time,

Date Now = new date ();

Now. gettime (); The returned long value is correct, but once I parse now as a string, it will be 8 hours less, so set the following time zone. Keep it for future use.

 

Private long getnowtime (){
Dateformat format = getdateformat ();
Date Now = new date ();
Log. D ("Joey", "------- now time --- no format:" + now. gettime ());
String now_time = format. Format (now );
Log. D ("Joey", "------- now time:" + now_time );
Return now. gettime ();
// The current time is returned in milliseconds, but the Chinese format is 8 hours less.
}

Private dateformat getdateformat (){
Dateformat my_format = new simpledateformat ("yyyy/mm/dd hh: mm: SS ");
Timezone China = timezone. gettimezone ("Asia/Shanghai ");
My_format.settimezone (China );
Return my_format;
}
 
Private long getopentime (string date) {// obtain the start time of the activity in the format of yyyy/mm/dd hh: mm: Ss.
// Dateformat format2 = dateformat. getdateinstance ();
// Timezone China = timezone. gettimezone ("Asia/Shanghai ");
// Format2.settimezone (China );
Long a = 0;
Try {
Date date1 = getdateformat (). parse (date );
A = date1.gettime ();
} Catch (parseexception e ){
E. printstacktrace ();
}
Return;

}

In this way, the difference between the current time and the input date will not go wrong.

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.