2017-02-06 197 views
0

我正在尋找使用日曆對象顯示日期。使用日曆對象顯示日期

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類創建我的日期對象。

當我調試時,我注意到出生日期的顯示是錯誤的;它在我的生日對象中打印所有內容。我不知道該怎麼做。將不勝感激的幫助。 :)

回答

0

因爲您正在打印日曆對象變量的生日是。

您可能想要基於日曆實現自己的類,但是實際上可以實現您所期望的toString方法。

2

首先您應該使用java.time而不是Calendar

第二件事是你不必兩次創建你的日期(你正在做你的二傳手)。

最後但並非最不重要的,你有你打印(在toString()法)之前格式化日期

這是你的二傳手應該如何看起來像:

public void setBirthday(int month, int day, int year) { 
    this.birthday.set(Calendar.MONTH, month); 
    this.birthday.set(Calendar.DAY_OF_MONTH, day); 
    this.birthday.set(Calendar.YEAR, year); 
} 

甚至更​​好:

public void setBirthday(Calendar birthday) { 
    this.birthday = birthday; 
} 

這是你的toString()方法應該什麼樣子:

public String toString(){ 
    return "ID Employee Number: " + idNumber + "\n" + "Employee name: " + firstName + " " 
      + lastName + "\n" + "Birth date: " + formatter.format(birthday) + "\n"; 
} 

所以你還需要添加到您的代碼:

private final static SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); 
+1

的SimpleDateFormat不是線程安全的,所以最好是不要聲明此字段作爲靜態字段。最好的解決方案是使用JodaTime格式化程序,因爲它是線程安全的。如果沒有喬達時間更好的方法是每次你需要使用它的方法的上下文,或在格式的靜態實例同步時創建的SimpleDateFormat實例。 – Pavel

+0

或者只是將格式化程序聲明爲實例成員。但是建議使它成爲本地對象更好。 –

1

TL;博士

LocalDate birthdate; 
… 
this.birthdate = LocalDate.of(1955 , 3 , 17); // March 17, 1955. 

詳細

Answer by qwerty1423是正確的,應該被接受。您正在看到toString方法的結果。如果你堅持使用Calendar,學習以你想要的格式生成一個字符串。

java.time

但你不應該使用Calendar。這是麻煩的舊日期時間類之一,現在是遺留的,被java.time類取代。遺留的類是一個糟糕的混亂;避免它們。另外,java.time類使用immutable objects,因此是thread-safe

LocalDate類代表沒有時間和不帶時區的僅有日期的值。

public abstract class Employee implements EmployeeInfo { 

    protected String firstName; 
    protected String lastName; 
    protected String idNumber; 
    LocalDate birthday; 

    // Constructor 
    public Employee() { 
     this.firstName = ""; 
     this.lastName = ""; 
     this.idNumber = ""; 
     this.birthday = LocalDate.MIN ; // Or some other arbitrary default value. 
    } 

    // Constructor 
    public Employee(String first, String last, String id, int month, int day, int year) 
    { 
     this.firstName = first; 
     this.lastName = last; 
     this.idNumber = id; 
     this.birthdate = LocalDate.of(year , month , day) ; 
    } 

呼叫LocalDate::toString生成標準ISO 8601格式的字符串:YYYY-MM-DD。對於其他格式,請爲java.time.format.DateTimeFormatter類搜索堆棧溢出。

要本地化,指定:

  • FormatStyle,以確定該字符串應該是多長或縮寫是。
  • Locale確定(a)用於翻譯日期名稱,月份名稱等的人類語言,以及(b)用於確定縮寫,大寫,標點符號,分隔符等問題的文化規範。

實施例:

Locale l = Locale.CANADA_FRENCH ; 
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(l); 
String output = this.birthdate.format(f); 

見本code run live at IdeOne.com

birthdate.toString():1955年3月17日

輸出:jeudi 17個火星1955年

提示:

  • 訂貨時請務必使用意義的遞減順序您的日期時間部分:年,月,日,時,分,秒。這是合乎邏輯的,易於遵循,並遵循日期時間值的標準ISO 8601的風格。因此,不要通過int month, int day, int year作爲參數,請通過int year , int month , int dayOfMonth

  • 更好的是,只需傳遞一個LocalDate對象而不是單純的整數。這樣做使您的代碼更多self-documenting,確保有效的值,並提供type safety


關於java.time

java.time框架是建立在Java 8和更高版本。這些類代替了日期時間類legacy,如java.util.Date,Calendar,& SimpleDateFormat

Joda-Time項目,現在在maintenance mode,建議遷移到java.time類。請參閱Oracle Tutorial。並搜索堆棧溢出了很多例子和解釋。規格是JSR 310

使用符合JDBC 4.2或更高版本的JDBC driver,您可以直接與您的數據庫交換對象對象。不需要字符串和java.sql。*類。

從何處獲取java.time類?

ThreeTen-Extra項目與其他類擴展java.time。這個項目是未來可能增加java.time的一個試驗場。您可以在這裏找到一些有用的類,如Interval,YearWeek,YearQuartermore

相關問題