您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JAVA中获取当前系统时间

2025/9/28 2:47:25发布21次查看
一. 获取当前系统时间和日期并格式化输出:
import java.util.date;
import java.text.simpledateformat;
public class nowstring {
public static void main(string[] args) {
simpledateformat df = new simpledateformat(yyyy-mm-dd hh:mm:ss);//设置日期格式
system.out.println(df.format(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';
system.out.println(rs.getstring(convertbookdate));
2、利用simpledateformat类:
先要输入两个java包:
import java.util.date;
import java.text.simpledateformat;
然后:
定义日期格式:simpledateformat sdf = new simpledateformat(yy-mm-dd);
sql语句为:string sqlstr = select bookdate from roombook where bookdate between '2007-4-10' and '2007-4-25';
输出:
system.out.println(df.format(rs.getdate(bookdate)));
************************************************************
java中获取当前日期和时间的方法
 import java.util.date;
import java.util.calendar;
import java.text.simpledateformat;
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 = dateformat.format( now );
system.out.println(hehe);
calendar c = calendar.getinstance();//可以对每个时间域单独修改
int year = c.get(calendar.year);
int month = c.get(calendar.month);
int date = c.get(calendar.date);
int hour = c.get(calendar.hour_of_day);
int minute = c.get(calendar.minute);
int second = c.get(calendar.second);
system.out.println(year + / + month + / + date + +hour + : +minute + : + second);
}
}
有时候要把string类型的时间转换为date类型,通过以下的方式,就可以将你刚得到的时间字符串转换为date类型了。
simpledateformat sdf=new simpledateformat(yyyy-mm-dd);
java.util.date time=null;
try {
   time= sdf.parse(sdf.format(new date()));
} catch (parseexception e) {
e.printstacktrace();
}
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product