本篇文章给大家谈谈j *** a获取 *** 时间,以及j *** a获取随机数整数对应的知识点,文章可能有点长,但是希望大家可以阅读完,增长自己的知识,最重要的是希望对各位有所帮助,可以解决了您的问题,不要忘了收藏本站喔。
本文目录
一、j *** a如何获取当前时间 年月日 时分秒
j *** a如何获取当前时间以及格式化需要用到两个类,如下图:
1.获取当前时间,并格式化为(年-月-日时:分:秒)。
SimpleDateFor *** t df= new SimpleDateFor *** t("yyyy-MM-dd HH:mm:ss");
System.out.println(df.for *** t(t));
2.将j *** a.util.Date转换为j *** a.sql.Date格式。
j *** a.sql.Date sqld= new j *** a.sql.Date(t.getTime());
j *** a.sql.Time sqlt= new j *** a.sql.Time(t.getTime());
j *** a.sql.Timestamp sqlts= new j *** a.sql.Timestamp(t.getTime());
J *** a是一种广泛使用的计算机编程语言,拥有跨平台、面向对象、泛型编程的特 *** ,广泛应用于企业级Web应用开发和移动应用开发。
J *** a编程语言的风格十分接近C++语言。继承了C++语言面向对象技术的核心,舍弃了容易引起错误的指针,以引用取代;移除了C++中的运算符重载和多重继承特 *** ,用接口取代;增加垃圾回收器功能。
J *** a编程语言是个简单、面向对象、分布式、解释 *** 、健壮、安全与 *** 无关、可移植、高 *** 能、多线程和动态的语言。
二、j *** a 如何获取 *** 运行时间
*** 一:用j *** a.util.Date类来实现,并结合j *** a.text.DateFor *** t类来实现时间的格式化,看下面代码:
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-1620:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.j *** a
publi *** taticvoid *** in(String[]args){
DateFor *** td1=DateFor *** t.getDateInstance();//默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-1620:54:53)
DateFor *** td2=DateFor *** t.getDateTimeInstance();
DateFor *** td3=DateFor *** t.getTimeInstance();
DateFor *** td4=DateFor *** t.getInstance();//使用SHORT风格显示日期和时间
DateFor *** td5=DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL);//显示日期,周,时间(精确到秒)
DateFor *** td6=DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG);//显示日期。时间(精确到秒)
DateFor *** td7=DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT);//显示日期,时间(精确到分)
DateFor *** td8=DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM);//显示日期,时间(精确到分)
Stringstr8=d8.for *** t(now);//与SHORT风格相比,这种方式更好用
System.out.println("用Date方式显示时间:"+now);//此 *** 显示的结果和Calendar.getInstance().getTime()一样
System.out.println("用DateFor *** t.getDateInstance()格式化时间后为:"+str1);
System.out.println("用DateFor *** t.getDateTimeInstance()格式化时间后为:"+str2);
System.out.println("用DateFor *** t.getTimeInstance()格式化时间后为:"+str3);
System.out.println("用DateFor *** t.getInstance()格式化时间后为:"+str4);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL)格式化时间后为:"+str5);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG)格式化时间后为:"+str6);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT)格式化时间后为:"+str7);
System.out.println("用DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM)格式化时间后为:"+str8);
用Date方式显示时间: Thu Sep 17 09:39:46 CST 2015
用DateFor *** t.getDateInstance()格式化时间后为:2015-9-17
用DateFor *** t.getDateTimeInstance()格式化时间后为:2015-9-17 9:39:46
用DateFor *** t.getTimeInstance()格式化时间后为:9:39:46
用DateFor *** t.getInstance()格式化时间后为:15-9-17上午9:39
用DateFor *** t.getDateTimeInstance(DateFor *** t.FULL,DateFor *** t.FULL)格式化时间后为:2015年9月17日星期四上午09时39分46秒 CST
用DateFor *** t.getDateTimeInstance(DateFor *** t.LONG,DateFor *** t.LONG)格式化时间后为:2015年9月17日上午09时39分46秒
用DateFor *** t.getDateTimeInstance(DateFor *** t.SHORT,DateFor *** t.SHORT)格式化时间后为:15-9-17上午9:39
用DateFor *** t.getDateTimeInstance(DateFor *** t.MEDIUM,DateFor *** t.MEDIUM)格式化时间后为:2015-9-17 9:39:46
*** 二:用j *** a.util.Calendar类来实现,看下面:
//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单
publi *** taticvoid *** in(String[]args){
Calendarca=Calendar.getInstance();
intyear=ca.get(Calendar.YEAR);//获取年份
intmonth=ca.get(Calendar.MONTH);//获取月份
intday=ca.get(Calendar.DATE);//获取日
intminute=ca.get(Calendar.MINUTE);//分
intho *** =ca.get(Calendar.HOUR);//小时
intsecond=ca.get(Calendar.SECOND);//秒
intWeekOfYear=ca.get(Calendar.DAY_OF_WEEK);
System.out.println("用Calendar.getInstance().getTime()方式显示时间:"+ca.getTime());
System.out.println("用Calendar获得日期是:"+year+"年"+month+"月"+day+"日");
System.out.println("用Calendar获得时间是:"+ho *** +"时"+minute+"分"+second+"秒");
System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)
用Calendar.getInstance().getTime()方式显示时间: Thu Sep 17 09:40:40 CST 2015
用Calendar获得日期是:2015年8月17日
用Calendar获得时间是:9时40分40秒
总结:中的来说, *** 二是最方便的, *** 一显得分笨拙,不过看个人喜欢了。
三、j *** a程序:获取当前的 *** 时间
一.获取当前 *** 时间和日期并格式化输出:
import j *** a.text.SimpleDateFor *** t;
public static void *** in(String[] args){
SimpleDateFor *** t df= new SimpleDateFor *** t("yyyy-MM-dd HH:mm:ss");//设置日期格式
System.out.println(df.for *** t(new Date()));// new Date()为获取当前 *** 时间
二.在数据库里的日期只以年-月-日的方式输出,可以用下面两种 *** :
String sqlst="select convert(varchar(10),bookDate,126) as
convertBookDate from roomBook where bookDate between'2007-4-10' and
System.out.println(rs.getString("convertBookDate"));
import j *** a.text.SimpleDateFor *** t;
定义日期格式:SimpleDateFor *** t sdf= new SimpleDateFor *** t(yy-MM-dd);
sql语句为:String sqlStr="select bookDate from roomBook where bookDate between'2007-4-10' and'2007-4-25'";
System.out.println(df.for *** t(rs.getDate("bookDate")));
************************************************************
j *** a中获取当前日期和时间的 ***
import j *** a.text.SimpleDateFor *** t;
public static void *** in(String[] args){
SimpleDateFor *** t dateFor *** t= new SimpleDateFor *** t("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
String hehe= dateFor *** t.for *** t( now);
Calendar c= Calendar.getInstance();//可以对每个时间域单独修改
int year= c.get(Calendar.YEAR);
int month= c.get(Calendar.MONTH);
int date= c.get(Calendar.DATE);
int ho *** = c.get(Calendar.HOUR_OF_DAY);
int minute= c.get(Calendar.MINUTE);
int second= c.get(Calendar.SECOND);
System.out.println(year+"/"+ month+"/"+ date+""+ho *** +":"+minute+":"+ second);
有时候要把String类型的时间转换为Date类型,通过以下的方式,就可以将你刚得到的时间字符串转换为Date类型了。
SimpleDateFor *** t sdf=new SimpleDateFor *** t("yyyy-MM-dd");
time= sdf.parse(sdf.for *** t(new Date()));
关于本次j *** a获取 *** 时间和j *** a获取随机数整数的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。