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 ();
}