導航:首頁 > 編程語言 > datejavaformat

datejavaformat

發布時間:2022-05-20 21:52:18

『壹』 java中DateFormat類的相關問題~~~

我更喜歡用DateFormate的子類 SimpleDateFormat來定義格式和解析時間,而且代碼更清晰、簡潔

publicclassDateFormat01{
publicstaticvoidmain(String[]args){
Datedate=newDate();
SimpleDateFormatspf=newSimpleDateFormat("yyyy年-MM月-dd日:HH時-mm分-ss秒");
System.out.println(spf.format(date));
}
}

格式可以自己隨意寫

『貳』 請問Java中DateFormat df=new simpleDateFormat()是什麼意思

DateFormat和SimpleDateFormat都是java.text包下的類,兩者的關系是:
DateFormat是抽象類,SimpleDateFormat是具體類,DateFormat是SimpleDateFormat的父類。

由於DateFormat是抽象類,因此沒法用new來構建。而SimpleDateFormat可以。
Java的多態性,決定了SimpleDateFormat的類對象可以向上轉型為DateFormat類型,因此這句是可以的。
(另外,類名首字母都是大寫,你寫的simpleDateFormat的首字母寫錯了)

『叄』 請教關於linux下java類DateFormat的使用

1:DateDemo1類

[java] view plain
/*
* 取得系統的時間
*/
public class DateDemo1 {
public static void main(String args[]) {
System.out.println(System.currentTimeMillis());

}

輸出為:1311060393171
執行結果會顯示從1970年1月1日開始到取得系統時間為止所經過的毫秒數,例如1115346430703這個數字,但這樣的數字沒有人確切了解它的意 義是什麼,您可以使用Date類別來讓這個數字變的更有意義一些

2:DateDemo2類

[java] view plain
public class DateDemo2 {
@SuppressWarnings("deprecation")
public static void main(String args[]) {
Date date = new Date();

System.out.println("date.toString: " + date.toString());
System.out.println("date: " + date.getDate());
System.out.println("day: " + date.getDay());
System.out.println("hour: " + date.getHours());
System.out.println("minutes: " + date.getMinutes());
System.out.println("month: " + (date.getMonth() + 1));
System.out.println("seconds: " + date.getSeconds());
System.out.println("time: " + date.getTime());
System.out.println("timezone: " + date.getTimezoneOffset());
System.out.println("year: " + (date.getYear() + 1900));

Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);

System.out.println("----------Calendar-------");
System.out.println("year: " + year);
System.out.println("month: " + month);
System.out.println("day: " + day);
System.out.println("hour: " + hour);
System.out.println("minute: " + minute);
System.out.println("second: " + second);

}
}
用date獲得時間的值,如時、分、秒、月、日、年得方法已經過時,需要改用Calendar的方法來獲取時間的值

輸出結果為:

[java] view plain
date.toString: Tue Jul 19 15:32:42 CST 2011
date: 19
day: 2
hour: 15
minutes: 32
month: 7
seconds: 42
time: 1311060762362
timezone: -480
year: 2011
----------Calendar-------
year: 2011
month: 7
day: 19
hour: 3
minute: 32
second: 42

3:DateDemo3類

[java] view plain
public class DateDemo3 {
public static void main(String args[]) {
Date date = new Date();

/*
* DateFormat會依電腦上的區域設定顯示時間格式,EE表示星期,MM表示月份、dd表示日期,而yyyy是西元,每個字元的設定都各有其意義
* 從Date-->String
*/
DateFormat dateFormat = new SimpleDateFormat("EE-MM-dd-yyyy");
System.out.println(dateFormat.format(date));

DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(format1.format(date));

DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format2.format(date));

/*
* 從String-->Date
*/
String dateStr = "2011-7-19";
try {
Date dateTrans = format1.parse(dateStr);
//System.out.println(dateTrans.toString());
System.out.println(dateTrans.toLocaleString()); //轉換為本地的形式
} catch (ParseException e) {
e.printStackTrace();
}

}
}
使用DateFormat來格式化日期數據,上面部分為從Date-->String,下面的部分為從String-->Date

輸出結果為:

[java] view plain
星期二-07-19-2011
2011-07-19
2011-07-19 15:33:27
2011-7-19 0:00:00

4:DateDemo4類

[java] view plain
public class DateDemo4 {

public static void main(String[] args) {

Date date = new Date();
DateFormat shortFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
DateFormat mediumFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
DateFormat longFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
DateFormat fullFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);

System.out.println(shortFormat.format(date));
System.out.println(mediumFormat.format(date));
System.out.println(longFormat.format(date));
System.out.println(fullFormat.format(date));

}

}

輸出結果為:

[java] view plain
11-7-19 下午3:33
2011-7-19 15:33:54
2011年7月19日 下午03時33分54秒
2011年7月19日 星期二 下午03時33分54秒 CST

5:DateDemo5類

[java] view plain
public class DateDemo5 {
public static void main(String args[]) {
Date date = new Date();

Locale locale = new Locale("en", "US");

DateFormat shortDateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
DateFormat mediumDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
DateFormat longDateFormat = DateFormat.getDateInstance(DateFormat.LONG, locale);
DateFormat fullDateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);

System.out.println(shortDateFormat.format(date));
System.out.println(mediumDateFormat.format(date));
System.out.println(longDateFormat.format(date));
System.out.println(fullDateFormat.format(date));

}
}

輸出結果為:

[java] view plain
7/19/11
Jul 19, 2011
July 19, 2011
Tuesday, July 19, 2011

『肆』 java DateFormat,format空指針異常

造成這個問題的原因是對象本身為null,確調用了對象的方法,所以造成空指針了。

DateFormat df1=null;
DateFormat df2=null;

這兩個對象都是null,你使用這兩個對象去掉用它的方法就會包空指針異常。

dfi.format(new Date())就相當於null.format(new Date()),所以會空指針。

解決方法:給df1和df2賦值,修改後如下:

DateFormatdf1=null;
DateFormatdf2=null;
df1=DateFormat.getDateInstance();
df2=DateFormat.getDateTimeInstance();
System.out.println(df1.format(newDate()));
System.out.println(df2.format(newDate()));

這樣就沒問題了。

『伍』 java,SimpleDateFormat的format()方法

java中SimpleDateFormat的format()方法的使用詳解:
public class SimpleDateFormat extends DateFormat
SimpleDateFormat 是一個以國別敏感的方式格式化和分析數據的具體類。 它允許格式化 (date -> text)、語法分析 (text -> date)和標准化。
SimpleDateFormat 允許以為日期-時間格式化選擇任何用戶指定的方式啟動。 但是,希望用 DateFormat 中getTimeInstance、 getDateInstance 或 getDateTimeInstance 創建一個日期-時間格式化程序。 每個類方法返回一個以預設格式化方式初始化的日期/時間格式化程序。 可以根據需要用 applyPattern 方法修改格式化方式。

SimpleDateFormat函數的繼承關系:
java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
舉例如下:
import java.text.*;
import java.util.Date;
/**
SimpleDateFormat函數語法:

G 年代標志符
y 年
M 月
d 日
h 時 在上午或下午 (1~12)
H 時 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第幾天
F 一月中第幾個星期幾
w 一年中第幾個星期
W 一月中第幾個星期
a 上午 / 下午 標記符
k 時 在一天中 (1~24)
K 時 在上午或下午 (0~11)
z 時區
*/
public class FormatDateTime {
public static void main(String[] args) {
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價於now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E ");
SimpleDateFormat myFmt4=new SimpleDateFormat(
"一年中的第 D 天 一年中第w個星期 一月中第W個星期 在一天中k時 z時區");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
}

}

效果:
2004年12月16日 17時24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17時24分27秒 星期四
一年中的第 351 天 一年中第51個星期 一月中第3個星期 在一天中17時 CST時區
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004

下面是個JavaBean:
public class FormatDateTime {

public static String toLongDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E ");
return myFmt.format(dt);
}

public static String toShortDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH時mm分");
return myFmt.format(dt);
}

public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
}
public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
}

public static void main(String[] args) {
Date now=new Date();
System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime.toShortTimeString(now));
}

}
調用的main 測試結果:
2015年6月4日 12時38分26秒 星期四
04年12月16日 17時38分
17 38 26 0965
04/12/16 17:38

『陸』 java 中simpleDateFormat 格式化時間的方法

java中SimpleDateFormat的format()方法的使用詳解:
public class SimpleDateFormat extends DateFormat
SimpleDateFormat 是一個以國別敏感的方式格式化和分析數據的具體類。 它允許格式化 (date -> text)、語法分析 (text -> date)和標准化。
SimpleDateFormat 允許以為日期-時間格式化選擇任何用戶指定的方式啟動。 但是,希望用 DateFormat 中getTimeInstance、 getDateInstance 或 getDateTimeInstance 創建一個日期-時間格式化程序。 每個類方法返回一個以預設格式化方式初始化的日期/時間格式化程序。 可以根據需要用 applyPattern 方法修改格式化方式。

SimpleDateFormat函數的繼承關系:
java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
舉例如下:
import java.text.*;
import java.util.Date;
/**
SimpleDateFormat函數語法:

G 年代標志符
y 年
M 月
d 日
h 時 在上午或下午 (1~12)
H 時 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第幾天
F 一月中第幾個星期幾
w 一年中第幾個星期
W 一月中第幾個星期
a 上午 / 下午 標記符
k 時 在一天中 (1~24)
K 時 在上午或下午 (0~11)
z 時區
*/
public class FormatDateTime {
public static void main(String[] args) {
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價於now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E ");
SimpleDateFormat myFmt4=new SimpleDateFormat(
"一年中的第 D 天 一年中第w個星期 一月中第W個星期 在一天中k時 z時區");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
}

}

效果:
2004年12月16日 17時24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17時24分27秒 星期四
一年中的第 351 天 一年中第51個星期 一月中第3個星期 在一天中17時 CST時區
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004

下面是個JavaBean:
public class FormatDateTime {

public static String toLongDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒 E ");
return myFmt.format(dt);
}

public static String toShortDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH時mm分");
return myFmt.format(dt);
}

public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
}
public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
}

public static void main(String[] args) {
Date now=new Date();
System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime.toShortTimeString(now));
}

}
調用的main 測試結果:
2015年6月4日 12時38分26秒 星期四
04年12月16日 17時38分
17 38 26 0965
04/12/16 17:38

『柒』 java dateformat怎麼用來轉換時間的表達格式

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HHmm");
System.out.println(sdf.format(new Date()));

『捌』 如何將JAVA DATE類型的日期 轉換成指定格式類型的 (如:YYYY-MM-DD) 的 DATE類型數據

Date類型並沒有格式,只有轉換成String格式的時候讓格式化顯示。

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")format(new Date());

Calendar calendar = Calendar.getInstance();

int year = Integer.parseInt(datetime.substring(0,4));

int month = Integer.parseInt(datetime.substring(5,7));

int date = Integer.parseInt(datetime.substring(8,10));

int hour = Integer.parseInt(datetime.substring(11,13));

int minute = Integer.parseInt(datetime.substring(14,16));

//int second = Integer.parseInt(datetime.substring(17,19));

if(calendar.get(Calendar.YEAR)>year){

int y = calendar.get(Calendar.YEAR)-year;

(8)datejavaformat擴展閱讀:

Date類可以在java.util包中找到,用一個long類型的值表示一個指定的時刻。它的一個有用的構造函數是Date(),創建一個表示創建時刻的對象。getTime()方法返回Date對象的long值。

import java.util.*;

public class Now {

public static void main(String[] args) {

Date now = new Date();

long nowLong = now.getTime();

System.out.println("Value is " + nowLong);

『玖』 java DateFormat類的.setTimeZone(TimeZone zone)怎樣使用

setTimeZone
publicvoidsetTimeZone(TimeZonezone)
為此DateFormat對象的日歷設置時區。

參數:
zone-給定的新時區。

TimeZone

getDefault
()
獲取此主機的默認TimeZone。默認TimeZone的來源可能隨實現的變化而變化。

返回:
默認的TimeZone。
另請參見:
setDefault(java.util.TimeZone)
getTimeZone
(StringID)
獲取給定ID的TimeZone。

參數:
ID-TimeZone的ID,要麼是縮寫(如"PST"),要麼是全名(如"America/Los_Angeles"),要麼是自定義ID(如"GMT-8:00")。注意,對縮寫的支持只是出於JDK1.1.x兼容性的考慮,因此應該使用全名。
返回:
指定的TimeZone,如果給定的ID無法理解,則返回GMT區域。
DateFormatdateFormat=newSimpleDateFormat();
//dateFormat.setTimeZone(TimeZone.getDefault());
dateFormat.setTimeZone(TimeZone.getTimeZone("EET"));
System.out.println(dateFormat.format(newDate()));

『拾』 java 高手請進!利用DateFormat來解析年月日!

import java.text.DateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
DateFormat df = DateFormat.getInstance();
String[] strings = df.format(new Date()).split(" ")[0].split("-");
for (int i = 0; i < strings.length; i++) {
//i=0 年份,i=1 月份,i=2 日可以進行判斷
System.out.println(strings[i]);
}
}
}

閱讀全文

與datejavaformat相關的資料

熱點內容
貴陽螺桿壓縮冷凝機組組成 瀏覽:115
掃描製作pdf 瀏覽:515
2016奇駿車機如何安裝app 瀏覽:764
phpvc9x64 瀏覽:73
蜜語星球解壓 瀏覽:476
c語言編譯器怎麼打不開 瀏覽:818
海印程序員 瀏覽:648
為什麼華為手機找不到伺服器 瀏覽:664
pdf增減 瀏覽:608
雲伺服器怎麼公網架設網站 瀏覽:91
pythonrequests慢 瀏覽:140
excel保存沒有pdf 瀏覽:922
冰箱壓縮機管囗示意圖 瀏覽:497
許振民編譯局 瀏覽:625
雙網路加什麼伺服器好用 瀏覽:211
linux命令中文 瀏覽:839
python怎麼做物聯網 瀏覽:731
app有什麼推薦嗎 瀏覽:79
自學程序員能不能面試工作 瀏覽:879
有錢人的解壓方法 瀏覽:84