Java obtains the system time

Source: Internet
Author: User
Tags date now

1. New java. util. Date ()

2. Calendar c = calendar. getinstance ();
C. settime (New java. util. Date ());
Int year = C. Get (calendar. year );
Int month = C. Get (calendar. month) + 1;
Int day = C. Get (calendar. day_of_month );
Int hour = C. Get (calendar. hour_of_day );
Int minute = C. Get (calendar. Minute );
Int second = C. Get (calendar. Second );

3. Date = new date ();
Date. gethours ();
Date. getseconds ();

4. Instance

//--------------------------------------------

Import java. util .*;

Import java. Text .*;

Public class nowstring {

Public static void main (string [] ARGs ){

Date Now = new date ();

Dateformat d = dateformat. getdateinstance ();

String STR = D. Format (now );

System. Out. println ("today is" + Str );

}

}

//--------------------------------------------

Add a simple sentence: String date = new simpledateformat ("yyyy-mm-dd hh: mm: SS"). Format (calendar. getinstance (). gettime ());
Date is the time you get. The display format can be adjusted randomly according to the format ("yyyy-mm-dd hh: mm: SS, if you want to get some more specific date rows
You can view the calendar class of the API for data.
For example:
Package test;

Import java. Text. dateformat;
Import java. Text. simpledateformat;
Import java. util. calendar;
Import java. util. date;

Public class nowstring {
Public static void main (string [] ARGs ){

Calendar c = calendar. getinstance ();
Date = C. gettime ();
String STR = new simpledateformat ("yyyy-mm-dd"). Format (date );
System. Out. println (STR );

String indate = "2008-09-01 ";
Int I = indate. compareto (STR); // compare the size of two dates

If (I> 0 ){
System. Out. println ("valid:" + I );
} Else {
System. Out. println ("invalid:" + I );
}
}
}

5. The instance can be obtained through the following methods on the JSP page:

Function printdate (){

VaR today = new date ();

VaR day = today. getdate ();

VaR month = today. getmonth () + 1;

VaR year = today. getfullyear (). tostring ();

VaR box1 = Document. Stats. Invalid date;

If (month/10) <1 ){

Month = ("0" + month). tostring ();

}

If (day/10) <1 ){

Day = ("0" + today. getdate (). tostring ();

}

Box1.value = year + "-" + month + "-" + day ;}
 

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.