Java time-related operations

Source: Internet
Author: User

1. Time to format

//Control the time format, hh indicates a system of Hours,hh is a one-hour system

simpledateforma t sdf =  new "Yyyy-mm-dd hh:mm:ss :SSS "

DATE  Date=new   Date () .

//Date Turn String

String begin = Sdf.format (date);

//String turn Date

Date ABC =sdf.parse (begin);

2. Modification Time:

Calendar cal = Calendar. getinstance ();

Cal . SetTime (new Date ()); //Get current time ;

cal .add (Calendar. year , +2);// year plus 2

Cal . Add (Calendar. MONTH , +1);// monthly plus 1

Cal.add (Calendar.day_of_month, +7);

Cal.add (Calendar. HOUR , +23);

Cal.add (Calendar. MINUTE , +59);

Cal.add (Calendar. SECOND , +59);

Date D=cal.gettime ();


3, to find the difference in date:   

SimpleDateFormat df = New   SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");

Date begin = NULL ; //

Date end = NULL ; // if compared to the current time: cal.gettime ()

Try  {

begin = Df.parse ("2004-01-02 11:30:24");

end = Df.parse ("2005-01-02 11:31:25");

} catch (ParseException e) {

//  TODO auto-generated Catch block

E.printstacktrace ();

}

Long between= (End.gettime ()-begin.gettime ())/1000; //divided by 1000 for conversion into seconds

int int

int int )   (between% (24*3600)/3600); 

int minute= (int) (BETWEEN%3600/60); //remaining points

4. Set method default setting 23:59:59

Public void setEndtimeTw0(Date endtime) {

Calendar cal = Calendar. getinstance ();

Cal.settime (Endtime);

Cal.add (Calendar. HOUR , +23);

Cal.add (Calendar. MINUTE , +59);

Cal.add (Calendar. SECOND , +59);

This . Endtime = Cal.gettime ();

}


Java time-related operations

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.