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

Python014

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")))

 Properties props=System.getProperties() //获得系统属性集    

 String osName = props.getProperty("os.name") //操作系统名称    

 String osArch = props.getProperty("os.arch") //操作系统构架    

 String osVersion = props.getProperty("os.version") //操作系统版本  

 System.err.println(osName)

 System.err.println(osArch)

 System.err.println(osVersion)

首先获取当前时间:

java.util.Date nowdate = new java.util.Date()

2/2

然后如果你想时间的格式和你想用的时间格式一致 那么就要格式化时间了SimpleDateFormat 的包在java.text包下SimpleDateFormat

sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //年月日 时分秒

String t = sdf.parse(nowdate);