JAVA 如何得到系统当前时间
发布时间:2025-05-19 16:11:32 发布人:远客网络
一、JAVA 如何得到系统当前时间
1、要获取Java系统当前时间,首先需要导入相应的包。具体代码如下:
2、import java.text.SimpleDateFormat;
3、接下来,创建一个Date对象来表示当前时间:
4、然后,定义一个SimpleDateFormat对象来格式化日期和时间。这里使用"yyyy年MM月dd日"格式:
5、SimpleDateFormat m=new SimpleDateFormat("yyyy年MM月dd日");
6、如果你需要显示小时、分钟和秒,只需在"yyyy年MM月dd日"后面添加"HH:mm:ss",例如:
7、SimpleDateFormat m=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
8、记得根据需要调整时间格式,以满足具体需求。
9、这样,你就可以使用SimpleDateFormat对象的format方法来格式化Date对象,从而获取当前时间的特定格式表示。
10、String formattedTime= m.format(nowTime);
11、这个字符串变量formattedTime就包含了以指定格式表示的当前时间。
12、这种方法适用于Java中的日期和时间处理,能够帮助你轻松获取和格式化系统当前时间。
13、注意,SimpleDateFormat对象的构造函数中的格式字符串是区分大小写的,确保正确使用。
14、通过这种方式,你可以灵活地控制时间的显示格式,以适应不同的应用场景。
二、java程序:获取当前的系统时间
一.获取当前系统时间和日期并格式化输出:
import java.text.SimpleDateFormat;
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()为获取当前系统时间
二.在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:
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 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.text.SimpleDateFormat;
public static void main(String[] args){
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
String hehe= dateFormat.format( now);
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");
time= sdf.parse(sdf.format(new Date()));
三、Java中如何获取当前时间。
方法一:用java.util.Date类来实现,并结合java.text.DateFormat类来实现时间的格式化,看下面代码:
//以下默认时间日期显示方式都是汉语语言方式
//一般语言就默认汉语就可以了,时间日期的格式默认为MEDIUM风格,比如:2008-6-16 20:54:53
//以下显示的日期时间都是再Date类的基础上的来的,还可以利用Calendar类来实现见类TestDate2.java
public static void main(String[] args){
Calendar cal= Calendar.getInstance();
DateFormat d1= DateFormat.getDateInstance();//默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
DateFormat d2= DateFormat.getDateTimeInstance();
DateFormat d3= DateFormat.getTimeInstance();
DateFormat d4= DateFormat.getInstance();//使用SHORT风格显示日期和时间
DateFormat d5= DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL);//显示日期,周,时间(精确到秒)
DateFormat d6= DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);//显示日期。时间(精确到秒)
DateFormat d7= DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);//显示日期,时间(精确到分)
DateFormat d8= DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//显示日期,时间(精确到分)
String str8= d8.format(now);//与SHORT风格相比,这种方式最好用
System.out.println("用Date方式显示时间:"+ now);//此方法显示的结果和Calendar.getInstance().getTime()一样
System.out.println("用DateFormat.getDateInstance()格式化时间后为:"+ str1);
System.out.println("用DateFormat.getDateTimeInstance()格式化时间后为:"+ str2);
System.out.println("用DateFormat.getTimeInstance()格式化时间后为:"+ str3);
System.out.println("用DateFormat.getInstance()格式化时间后为:"+ str4);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为:"+ str5);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为:"+ str6);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后为:"+ str7);
System.out.println("用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间后为:"+ str8);
用Date方式显示时间: Mon Jun 16 20:54:53 CST 2008
用DateFormat.getDateInstance()格式化时间后为:2008-6-16
用DateFormat.getDateTimeInstance()格式化时间后为:2008-6-16 20:54:53
用DateFormat.getTimeInstance()格式化时间后为:20:54:53
用DateFormat.getInstance()格式化时间后为:08-6-16下午8:54
用DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL)格式化时间后为
:2008年6月16日星期一下午08时54分53秒 CST
用DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG)格式化时间后为
:2008年6月16日下午08时54分53秒
用DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT)格式化时间后
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化时间
方法二:用java.util.Calendar类来实现,看下面:
//以下是利用Calendar类来实现日期时间的,和Date类相比较比较简单
public static void main(String[] args){
Calendar ca= Calendar.getInstance();
int year= ca.get(Calendar.YEAR);//获取年份
int month=ca.get(Calendar.MONTH);//获取月份
int day=ca.get(Calendar.DATE);//获取日
int minute=ca.get(Calendar.MINUTE);//分
int hour=ca.get(Calendar.HOUR);//小时
int second=ca.get(Calendar.SECOND);//秒
int WeekOfYear= 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获得时间是:"+ hour+"时"+ minute+"分"+ second+"秒");
System.out.println(WeekOfYear);//显示今天是一周的第几天(我做的这个例子正好是周二,故结果显示2,如果你再周6运行,那么显示6)
用Calendar.getInstance().getTime()方式显示时间: Mon Jun 16 21:54:21 CST 2008
用Calendar获得日期是:2008年5月16日
用Calendar获得时间是:9时54分21秒