JAVA中获取系统当前时间该怎么写?

Python09

JAVA中获取系统当前时间该怎么写?,第1张

一. 获取当前系统时间日期并格式化输出:\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")))

java中的Date类,实例化即可获取当前日期:

(import java.util.Date

import java.text.SimpleDateFormat)

代码:

Date now = new Date()

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd")

String curDate= dateFormat.format( now )

java语言 获得系统当前日期:

1、Date date=new Date()这个是java提供的时间类,可以从中取出,年、月日、时、分、秒

2、SimpleDateFormat这个是时间格式类,对时间进行格式化

String time=new SimpleDateFormat("HH:mm:ss").format(new Date())

time=15:02:03

String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())

time=2015-05-26 15:02:03

3、System.currentTimeMillis(),返回的是long型日期时间

long time=System.currentTimeMillis();

time=352632563256