日历构造函数Java toString
我想要做的是将一个日期传递到日历,以便它将格式化日期准备好与另一个构造函数一起使用。所以我可以使用它以后使用日历提供的功能。
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
假设 DateD
是日历
,它是默认 toString()
实现。您需要调用 getTime()
才能得到日期
。
Assuming DateD
is Calendar
, it is default toString()
implementation. You need to call getTime()
to get the date
out of it.
返回此日历的字符串表示形式。此方法仅用于调试目的,并且返回的字符串的格式可能因实现而异。返回的字符串可以为空,但不能为空。
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 将其转换为 String
You can use SimpleDateFormat to convert it to String