DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")Date date = new Date()System.out.println(dateFormat.format(date))
2. Calender() + SimpleDateFormat() Calendar cal = Calendar.getInstance()
System.out.println(dateFormat.format(cal.getTime()))
举个例子给你:
import java.util.Date
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
public class GetCurrentDateTime {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
//get current date time with Date()
Date date = new Date()
System.out.println(dateFormat.format(date))
//get current date time with Calendar()
Calendar cal = Calendar.getInstance()
System.out.println(dateFormat.format(cal.getTime()))
}
}
别忘了import
一. 获取当前系统时间和日期并格式化输出:\x0d\x0a\x0d\x0aimport java.util.Date\x0d\x0aimport java.text.SimpleDateFormat\x0d\x0a\x0d\x0apublic class NowString { \x0d\x0a public static void main(String[] args) { \x0d\x0a SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")//设置日期格式\x0d\x0a System.out.println(df.format(new Date()))// new Date()为获取当前系统时间\x0d\x0a } \x0d\x0a} \x0d\x0a\x0d\x0a二. 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:\x0d\x0a\x0d\x0a1、用convert()转化函数:\x0d\x0a\x0d\x0aString sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'"\x0d\x0a\x0d\x0aSystem.out.println(rs.getString("convertBookDate"))\x0d\x0a\x0d\x0a2、利用SimpleDateFormat类:\x0d\x0a\x0d\x0a先要输入两个java包:\x0d\x0a\x0d\x0aimport java.util.Date\x0d\x0aimport java.text.SimpleDateFormat\x0d\x0a\x0d\x0a然后:\x0d\x0a\x0d\x0a定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd)\x0d\x0a\x0d\x0asql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'"\x0d\x0a\x0d\x0a输出:\x0d\x0a\x0d\x0aSystem.out.println(df.format(rs.getDate("bookDate")))首先数据库得添加有入职时间date这一列啊
java获取当日系统时间代码:
Data date =new Date()
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
String date = sdf.format(date)
添加到数据库的代码得用到jdbc,要导入驱动和c3p0的jar包,再链接数据库,添加数据即可。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)