JAVA中获取当前系统时间,日期并格式化输出

一.获取当前系统时间和日期并格式化输出:

importjava.util.Date;
importjava.text.SimpleDateFormat;

publicclassNowString{
publicstaticvoidmain(String[]args){
SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");//设置日期格式
System.out.println(df.format(newDate()));//newDate()为获取当前系统时间
}
}

二.在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:

1、用convert()转化函数:

Stringsqlst="selectconvert(varchar(10),bookDate,126)asconvertBookDatefromroomBookwherebookDatebetween'2007-4-10'and'2007-4-25'";

System.out.println(rs.getString("convertBookDate"));

2、利用SimpleDateFormat类:

先要输入两个java包:

importjava.util.Date;
importjava.text.SimpleDateFormat;

然后:

定义日期格式:SimpleDateFormatsdf=newSimpleDateFormat(yy-MM-dd);

sql语句为:StringsqlStr="selectbookDatefromroomBookwherebookDatebetween'2007-4-10'and'2007-4-25'";

输出:

System.out.println(df.format(rs.getDate("bookDate")));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章