//得到long类型当前时间
long l = System.currentTimeMillis()
//new日期对
Date date = new Date(l)
//转换提日期输出格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-
dd HH:mm:ss")System.out.println(dateFormat.format(date))
扩展资料
package com.ob
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
public class DateTest {
public static void main(String[] args) throws ParseException {
Calendar now = Calendar.getInstance()
System.out.println("年: " + now.get(Calendar.YEAR))
System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "")
System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH))
System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY))
System.out.println("分: " + now.get(Calendar.MINUTE))
System.out.println("秒: " + now.get(Calendar.SECOND))
System.out.println("当前时间毫秒数:" + now.getTimeInMillis())
System.out.println(now.getTime())
\t\tDate d = new Date()
System.out.println(d)
\t\tSimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
\t\tString dateNowStr = sdf.format(d)
System.out.println("格式化后的日期:" + dateNowStr)
\t\t
\t\tString str = "2012-1-13 17:26:33"
//要跟上面sdf定义的格式一样
\t\tDate today = sdf.parse(str)
System.out.println("字符串转成日期:" + today)
\t}
}
参考资料:Java - 百度百科
一. 获取当前系统时间和日期并格式化输出:\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")))