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");//Set the date format
System.out.println(df.format( new Date()));// new Date() is to get the current system time
}
}
2. The date in the database is only output in the form of year-month-day, and the following two methods can be used:
1. Use convert() to convert the function:
String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";
System.out.println(rs.getString("convertBookDate"));
2. Use the SimpleDateFormat class:
First, enter two java packages:
import java.util.Date;
import java.text.SimpleDateFormat;
Then:
Define the date format: SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);
sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";
output:
System.out.println(df.format(rs.getDate("bookDate")));