本文目錄一覽:
java怎麼獲取當前時間
JAVA中獲取當前系統時間關鍵代碼:
//得到long類型當前時間
long l = System.currentTimeMillis();
//new日期對象
Date date = new Date(l);
//轉換提日期輸出格式
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
System.out.println(dateFormat.format(date));
完整代碼:
package com.ob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
//得到long類型當前時間
long l = System.currentTimeMillis();
//new日期對象
Date date = new Date(l);
//轉換提日期輸出格式
SimpleDateFormat dateFormat = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
System.out.println(dateFormat.format(date));
}
}
輸出結果:
2017-01-06 12:28:19
Java代碼中如何獲得當前時間
有兩種方法:
方法一:用java.util.Date類來實現,並結合java.text.DateFormat類來實現時間的格式化,看下面代碼:
import java.util.*;
import java.text.*;
//以下默認時間日期顯示方式都是漢語語言方式
//一般語言就默認漢語就可以了,時間日期的格式默認為MEDIUM風格,比如:2008-6-16 20:54:53
//以下顯示的日期時間都是再Date類的基礎上的來的,還可以利用Calendar類來實現見類TestDate2.java
public class TestDate {
public static void main(String[] args) {
Date now = new Date();
Calendar cal = Calendar.getInstance();
DateFormat d1 = DateFormat.getDateInstance(); //默認語言(漢語)下的默認風格(MEDIUM風格,比如:2008-6-16 20:54:53)
String str1 = d1.format(now);
DateFormat d2 = DateFormat.getDateTimeInstance();
String str2 = d2.format(now);
DateFormat d3 = DateFormat.getTimeInstance();
String str3 = d3.format(now);
DateFormat d4 = DateFormat.getInstance(); //使用SHORT風格顯示日期和時間
String str4 = d4.format(now);
DateFormat d5 = DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL); //顯示日期,周,時間(精確到秒)
String str5 = d5.format(now);
DateFormat d6 = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG); //顯示日期。時間(精確到秒)
String str6 = d6.format(now);
DateFormat d7 = DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT); //顯示日期,時間(精確到分)
String str7 = d7.format(now);
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)格式化時間後
為:08-6-16 下午8:54
用DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM)格式化時間
後為:2008-6-16 20:54:53
方法二:用java.util.Calendar類來實現,看下面:
import java.util.*;
import java.text.*;
//以下是利用Calendar類來實現日期時間的,和Date類相比較比較簡單
public class TestDate2 {
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秒
2
總結:中的來說,方法二是最方便的,方法一顯得分笨拙,不過看個人喜歡了。
還有一種方法利用System.currentTimeMillis()也可以。
JAVA中獲取系統當前時間該怎麼寫?
一. 獲取當前系統時間和日期並格式化輸出:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0apublic class NowString { \x0d\x0a public static void main(String[] args) { \x0d\x0a SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);//設置日期格式\x0d\x0a System.out.println(df.format(new Date()));// new Date()為獲取當前系統時間\x0d\x0a } \x0d\x0a} \x0d\x0a\x0d\x0a二. 在資料庫里的日期只以年-月-日的方式輸出,可以用下面兩種方法:\x0d\x0a\x0d\x0a1、用convert()轉化函數:\x0d\x0a\x0d\x0aString sqlst = “select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between ‘2007-4-10’ and ‘2007-4-25′”;\x0d\x0a\x0d\x0aSystem.out.println(rs.getString(“convertBookDate”)); \x0d\x0a\x0d\x0a2、利用SimpleDateFormat類:\x0d\x0a\x0d\x0a先要輸入兩個java包:\x0d\x0a\x0d\x0aimport java.util.Date; \x0d\x0aimport java.text.SimpleDateFormat;\x0d\x0a\x0d\x0a然後:\x0d\x0a\x0d\x0a定義日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);\x0d\x0a\x0d\x0asql語句為:String sqlStr = “select bookDate from roomBook where bookDate between ‘2007-4-10’ and ‘2007-4-25′”;\x0d\x0a\x0d\x0a輸出:\x0d\x0a\x0d\x0aSystem.out.println(df.format(rs.getDate(“bookDate”)));
java中如何獲取本機當前時間
public static void main(String[] args)
{
ActionListener time = new ActionListener() { // 監聽事件,不然實現不了動態改變時間
public void actionPerformed(ActionEvent e) {
//date對象代表當前的系統時間(毫秒)
Date date = new Date();
//format對象是用來以指定的時間格式格式化時間的
SimpleDateFormat from = new SimpleDateFormat(
“yyyy-MM-dd HH:mm:ss”); //這裡的格式可以自己設置
//format()方法是用來格式化時間的方法
String times = from.format(date);
System.out.println(times); }
};
Timer tim = new Timer(1000, time); //這裡表示1000毫秒更新一下時間
tim.start(); //啟動
}
我這個答案肯定正確啊
下面幫你解釋你的答案吧
Date //是在java.util.Date;裡面
SimpleDateForma //這個是java.text.SimpleDateFormat;用來輸出問本格式的
DateFormat //應該是在java.util.*;裡面吧..應該是的
你那個錯誤是編譯就沒通過啊
public class Test
那你那個編譯寫的因該是
javac Test.java 編譯的應該是類啊而不是javac time.java 請問你的time什麼意思呢,所以你報的異常是找不到time類啊
呵呵…你是初學java吧該答的我都答完了拉!還特地幫你每句都寫了解釋
接下來你要給我分了吧
把我上面的那個代碼複製進去就OK拉
還有不懂的就補充問題啊
我會關注你的問題的!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/285547.html