我正在尋找使用日曆對象顯示日期。使用日曆對象顯示日期
public abstract class Employee implements EmployeeInfo {
protected String firstName;
protected String lastName;
protected String idNumber;
Calendar birthday = Calendar.getInstance();
protected char gender;
public Employee()
{
firstName = "";
lastName = "";
idNumber = "";
gender = ' ';
birthday.set(Calendar.MONTH, 0);
birthday.set(Calendar.DAY_OF_MONTH, 00);
birthday.set(Calendar.YEAR, 0000);
}
public Employee(String first, String last, String id, char gen, int month, int day, int year)
{
firstName = first;
lastName = last;
idNumber = id;
gender = gen;
birthday.set(Calendar.MONTH, month);
birthday.set(Calendar.DAY_OF_MONTH, day);
birthday.set(Calendar.YEAR, year);
}
public Calendar getBirthday() {
return birthday;
}
public void setBirthday(int month, int day, int year, Calendar birthday) throws ParseException {
birthday = Calendar.getInstance();
birthday.set(Calendar.MONTH, month);
birthday.set(Calendar.DAY_OF_MONTH, day);
birthday.set(Calendar.YEAR, year);
SimpleDateFormat formatted = new SimpleDateFormat("MM/dd/yyyy");
String date = month + "/" + day + "/" + year;
Date birth = formatted.parse(date);
birthday.setTime(birth);
this.birthday = birthday;
}
public String toSring()
{
return "ID Employee Number: " + idNumber + "\n" + "Employee name: " + firstName + " "
+ lastName + "\n" + "Birth date: " + birthday + "\n";
}
public abstract double getMonthlyEarning();
public class Staff extends Employee {
protected double hourlyRate;
public Staff()
{
super();
hourlyRate = 0.0;
}
public Staff(String first, String last, String ID, char gen1, int month, int day, int year, double rate)
{
super(first, last, ID, gen1, month, day, year);
hourlyRate = rate;
}
}
...和...
public class Test {
public static void main(String[] args) {
Employee[] employees = new Employee[2];
employees[0] = new Staff("Minh", "Vu", "123", 'M', 3,06,1997, 50.00);
employees[1] = new Staff("Mike", "Nguyen", "456", 'M', 5,18,1977, 65.00);
for(Employee member : employees)
{
System.out.println(member);
System.out.println("------------------------------------------");
}
}
}
我現在面臨的問題是,爲什麼在下面的輸出的出生日期給了我一個未知的,可笑的長線:
ID員工人數:123
員工名稱:Minh Vu
出生日期:java.util.GregorianCa lendar [時間= ?, areFieldsSet =假,areAllFieldsSet =真,寬大=真,區= sun.util.calendar.ZoneInfo [ID = 「美國/洛杉磯」,偏移= -28800000,dstSavings = 3600000,useDaylight =真,過渡= 185,lastRule = java.util.SimpleTimeZone中[ID =美洲/洛杉磯,偏移= -28800000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 3,startMonth = 2,朝九特派= 8,startDayOfWeek = 1, STARTTIME = 7200000,startTimeMode = 0,endMode = 3,endMonth = 10,endday指定= 1,一個endDayOfWeek = 1,結束時間= 7200000,endTimeMode = 0]],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 1997,月= 3,WEEK_OF_YEAR = 6,WEEK_OF_MONTH = 2,DAY_OF_MONTH = 6,DAY_OF_YEAR = 37,DAY_OF_WEEK = 2,DAY_OF_WEEK_IN_MONTH = 1,AM_PM = 1,HOUR = 2,HOUR_OF_DAY = 0,MINUTE = 0,SECOND = 0,微差= 0,ZONE_OFFSET = -28800000,DST_OFFSET = 0]
全職
月LY薪酬:$ 8000.0
ID員工人數:456
員工姓名:邁克·阮
出生日期:java.util.GregorianCalendar中的[時間= ?, areFieldsSet =假,areAllFieldsSet = TRUE,寬大=真,區= sun.util.calendar.ZoneInfo [ID = 「美國/洛杉磯」,偏移= -28800000,dstSavings = 3600000,useDaylight =真,過渡= 185,lastRule = java.util.SimpleTimeZone中[ID =美國/洛杉磯,偏移= -28800000,dstSavings = 3600000,useDaylight =真,startYear = 0,STARTMODE = 3,startMonth = 2,朝九特派= 8,startDayOfWeek = 1,開始時間= 7200000,startTimeMode = 0,endMode = 3,endMonth = 10,endday指定= 1,endday指定OfWeek = 1,結束時間= 7200000,endTimeMode = 0]],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 1977,MONTH = 5,WEEK_OF_YEAR = 6,WEEK_OF_MONTH = 2,DAY_OF_MONTH = 18,DAY_OF_YEAR = 37, DAY_OF_WEEK = 2,DAY_OF_WEEK_IN_MONTH = 1,AM_PM = 1,HOUR = 2,HOUR_OF_DAY = 0,MINUTE = 0,SECOND = 0,微差= 0,ZONE_OFFSET = -28800000,DST_OFFSET = 0]
全職
月薪:$ 10400.0
根據我的分析,我認爲,我必須使用創建SimpleDateFormat的類的對象,並把「MM/DD/YYYY」到參數。但是,我必須通過創建Date對象來解析SimpleDateFormat對象。我想用Calendar類創建我的日期對象。
當我調試時,我注意到出生日期的顯示是錯誤的;它在我的生日對象中打印所有內容。我不知道該怎麼做。將不勝感激的幫助。 :)
的SimpleDateFormat不是線程安全的,所以最好是不要聲明此字段作爲靜態字段。最好的解決方案是使用JodaTime格式化程序,因爲它是線程安全的。如果沒有喬達時間更好的方法是每次你需要使用它的方法的上下文,或在格式的靜態實例同步時創建的SimpleDateFormat實例。 – Pavel
或者只是將格式化程序聲明爲實例成員。但是建議使它成爲本地對象更好。 –