网站首页 语言 会计 互联网计算机 医学 学历 职场 文艺体育 范文
当前位置:学识谷 > IT认证 > JAVA认证

Java获取当前的系统时间的方法

栏目: JAVA认证 / 发布于: / 人气:2.7W

在java中,我们可以利用程序获取当前的'系统时间,那么你知道该怎么获取吗?下面跟yjbys小编一起来学习一下吧!

Java获取当前的系统时间的方法

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

import ;

import leDateFormat;

public class NowString {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

tln(at(new Date()));// new Date()为获取当前系统时间

}

}

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

1、用convert()转化函数:

String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

tln(tring("convertBookDate"));

2、利用SimpleDateFormat类:

先要输入两个java包:

import ;

import leDateFormat;

然后:

定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);

sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

输出:

tln(at(ate("bookDate")));

************************************************************

java中获取当前日期和时间的方法

import ;

import ndar;

import leDateFormat;

public class TestDate{

public static void main(String[] args){

Date now = new Date();

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式

String hehe = at( now );

tln(hehe);

Calendar c = nstance();//可以对每个时间域单独修改

int year = ();

int month = (H);

int date = ();

int hour = (_OF_DAY);

int minute = (TE);

int second = (ND);

tln(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);

}

}

有时候要把String类型的时间转换为Date类型,通过以下的方式,就可以将你刚得到的时间字符串转换为Date类型了。

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

time=null;

try {

time= e(at(new Date()));

}

catch (ParseException e) {

tStackTrace();

}