我可以使用适当的导入方法格式化CURRENT DATE日期,但格式化我自己的日期(例如我的生日)是另一回事。当使用 tostring 方法返回它时它可以工作,因为我手动格式化了它。无论如何,我的问题是:我可以使用导入来格式化我的生日吗?
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Date2 {
private int month;
private int day;
private int year;
private Date today = Calendar.getInstance().getTime();
// create a short version date formatter
private DateFormat shorFormatter = SimpleDateFormat
.getDateInstance(SimpleDateFormat.SHORT);
// create a long version of date format
private DateFormat longFormattter = SimpleDateFormat
.getDateInstance(SimpleDateFormat.LONG);
// create date time formatter, medium for day, long for time
private DateFormat mediuFormattter = SimpleDateFormat.getDateTimeInstance(
SimpleDateFormat.MEDIUM, SimpleDateFormat.LONG);
/************ Constructors ***************/
public Date2() {
}
public Date2(int dd, int mm, int yyyy) {
this.day = dd;
this.month = mm;
this.year = yyyy;
}
/************ Getters setters ************/
public Date getToday() {
return today;
}
public String getShorFormatter() {
return shorFormatter.format(today);
}
public String getLongFormattter() {
return longFormattter.format(today);
}
public String getMediuFormattter() {
return mediuFormattter.format(today);
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String tosString() {
return month + "/" + day + "/" + year;
}
}
public class DateFormatTest {
public static void main(String[] args) {
Date2 date2 = new Date2(23, 05, 1973);
System.out.println(date2.getShorFormatter());
System.out.println(date2.getLongFormattter());
System.out.println(date2.getMediuFormattter());
System.out.println(date2.tosString());
}
}