一. 获取当前
系统时间和
日期并格式化输出:\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想知道系统时间被更改可以查看系统的时间和日期,点击设置时间和日期即可查看。
java是一种计算机编程语言,拥有跨平台,面向对象,泛型编程的特性,广泛应用于企业级Web应用开发和移动应用开发,任职于太阳微系统的詹姆斯·高斯林等人于1990年代初开发Java语言的雏形,最初被命名为Oak,目标设置在家用电器等小型系统的程序语言。
利用java里的Date类输出,进阶的做法还可以用simpleDateformat类进行格式化输出日期。代码如下:
import java.text.SimpleDateFormat
import java.util.Date
/**
* 日期格式化
* @author young
*
*/
public class SimpleDateFormatTest {
public static void main(String[] args) {
// 在构造器中传入日期样式
// SimpleDateFormat sdf=new SimpleDateFormat(
// "yyyy.MM.dd G 'at' HH:mm:ss z")
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy")
// sdf=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒")
// 当前系统时间
Date date = new Date()
// 调用format(Date date)对象传入的日期参数进行格式化
// format(Date date)将日期转化成字符串
String formatDate = sdf.format(date)
System.out.println("格式化后的日期为:" + formatDate)
}
}