問題描述
我要做的是將日期傳遞到日歷中,以便它將格式化日期以供另一個構(gòu)造函數(shù)使用.這樣我以后可以使用日歷提供的功能來使用它.
What I'm trying to do is pass a date into the Calendar so that it will format the date ready for use with another constructor. So that i can make use of it later using the functions that calendar has provides.
public class Top {
public static void main(String[] args) {
Something st = new Something(getCalendar(20,10,2012));
System.out.println(st.toString());
}
public static Calendar getCalendar(int day, int month, int year){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, day);
return cal;
}
}
tostring 方法.
The tostring method.
public String toString(){
String s = "nDate: " + DateD;
return s;
}
日期:java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true
Date: java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true
而不是日期:20/10/2012
Rather than Date: 20/10/2012
推薦答案
假設(shè) DateD
為 Calendar
,則默認(rèn)為 toString()
實現(xiàn).您需要調(diào)用 getTime()
來獲取 date
.
Assuming DateD
is Calendar
, it is default toString()
implementation. You need to call getTime()
to get the date
out of it.
來自 日歷#toString()
返回此日歷的字符串表示形式.此方法僅用于調(diào)試目的,返回字符串的格式可能因?qū)崿F(xiàn)而異.返回的字符串可能為空但不能為空.
Return a string representation of this calendar. This method is intended to be used only for debugging purposes, and the format of the returned string may vary between implementations. The returned string may be empty but may not be null.
您可以使用 SimpleDateFormat 進(jìn)行轉(zhuǎn)換它到 String
You can use SimpleDateFormat to convert it to String
這篇關(guān)于日歷構(gòu)造函數(shù) Java toString的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!