Get the current system time in JAVA, java get the current system

Source: Internet
Author: User

Get the current system time in JAVA, java get the current system
 

1. Get the current system time and date and format the output:

Import java. util. Date;
Import java. text. SimpleDateFormat;

Public class NowString {
Public static void main (String [] args ){
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // you can specify the date format.
System. out. println (df. format (new Date (); // new Date () is used to obtain the current System time.
}
}

2. the date in the database is only output in the form of year-month-day. You can use the following two methods:

1. convert functions using convert:

String sqlst = "select convert (varchar (10), bookDate, 126) as convertBookDate from roomBook where bookDate between '2017-4-10' and '2017-4-25 '";

System. out. println (rs. getString ("convertBookDate "));

2. Use the SimpleDateFormat class:

Enter two java packages:

Import java. util. Date;
Import java. text. SimpleDateFormat;

Then:

Defines the Date Format: SimpleDateFormat sdf = new SimpleDateFormat (yy-MM-dd );

SQL statement: String sqlStr = "select bookDate from roomBook where bookDate between '2017-4-10 'and '2017-4-25 '";

Output:

System. out. println (df. format (rs. getDate ("bookDate ")));

 

**************************************** ********************

Java method for obtaining the current date and time

Import java. util. Date;
Import java. util. Calendar;

Import java. text. SimpleDateFormat;

Public class TestDate {
Public static void main (String [] args ){
Date now = new Date ();
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd HH: mm: ss"); // you can easily modify the date format.


String hehe = dateFormat. format (now );
System. out. println (hehe );

Calendar c = Calendar. getInstance (); // you can modify each time field separately.

 


Int year = c. get (Calendar. YEAR );
Int month = c. get (Calendar. MONTH );
Int date = c. get (Calendar. DATE );
Int hour = c. get (Calendar. HOUR_OF_DAY );
Int minute = c. get (Calendar. MINUTE );
Int second = c. get (Calendar. SECOND );
System. out. println (year + "/" + month + "/" + date + "" + hour + ":" + minute + ":" + second );
}
}

Sometimes you need to convert the Time of the String type to the Date type. In the following way, you can convert the time String you just obtained to the Date type.

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd ");

Java. util. Date time = null;
Try {
Time = sdf. parse (sdf. format (new Date ()));

} Catch (ParseException e ){

E. printStackTrace ();
}

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.