2016-05-09 9 views
0

私はレンタカー会社のインターフェースとクラスのセットを作成する必要がある割り当てを受けました。ライセンス番号は、3つのコンポーネントがあり廃止予定のメソッドを使用せずにDateオブジェクトから年を取得するにはどうすればよいですか?

:私は、次の仕様と一致している必要がありLicenceNumberクラスの実装に取り​​組んで忙しいです。最初のコンポーネントは、ドライバーの名の頭文字とドライバーの最後の名前の頭文字を連結したものです。 2番目の要素はライセンスの発行年です。 3番目の要素は任意のシリアル番号です。たとえば、1990年にMark Smithに発行されたライセンスのライセンス番号の文字列表現は、MS-1990-10の形式をとります。ここで、10はシリアル番号で、イニシャルと年を指定すると、ライセンス番号全体。

日付を表すには、java.util.Dateクラスを使用する必要があります。ただし、Dateクラスの非推奨メソッドは使用しないでください。たとえば、テストクラスでは、java.util.Calendarを使用して、生年月日とライセンス発行日を作成します。デフォルトのタイムゾーンとロケールを想定できます。 (Java 1.8で導入されたjava.timeパッケージではより良いクラスが利用できるようになりましたが、あまりうまく書かれていないクラス で作業するのは良い経験になるでしょう)。

これまでのところ、私はLicenceNumberクラスの次の実装を持っている:私はissueDateから年のみを取得できるようにしたい

import java.util.Calendar; 
import java.util.Date; 

public class LicenceNumber { 

private String licenceNo; 

public LicenceNumber(Name driverName, Date issueDate){ 
    setLicenceNo(driverName, issueDate); 
} 

public String getLicenceNo() { 
    return licenceNo; 
} 

public void setLicenceNo(Name driverName, Date issueDate) { 
    String initials; 
    initials = driverName.getForename().substring(0, 1) + driverName.getSurname().substring(0,1); 
    System.out.println(initials); 
    int issueYear = issueDate.getYear(); //Deprecated 
} 
} 

が、私はどのように把握することができます唯一の方法これは、推奨されないメソッドgetYear()を使用することです。これは明らかに基準に反しているので、廃止予定のメソッドを使用せずにDateオブジェクトからYearを取得する方法について誰かが気にすることはできますか?

ありがとうございます。

+5

割り当てに指定されているようにjava.util.Calendarを使用しようとしましたか? – antlersoft

+0

私の理解では、クラスのテストではCalendarユーティリティを使用することのみが許可されています。たとえば、getInstance()を使用してカレンダーを作成し、次に(YYYY、MM、DD)を使用してCalendarを設定し、getTime()を使用してDrivingLicenseに渡しました。 getTime()は、dateオブジェクトを作成します。 –

+0

'SimpleDateFormat 'のような書式設定クラスを使用することは許可されていますか? – KevinO

答えて

1

私はDateオブジェクトから年を得る3つの方法を考えていますが、廃止予定のメソッドは避けています。 2つのアプローチは他のオブジェクト(CalendarSimpleDateFormat)を使用し、第3のメソッドはDateオブジェクトの.toString()を解析します(そのメソッドはではなく、)。 .toString()はロケール固有の可能性があり、他のロケールではこのアプローチに問題がある可能性がありますが、今年は常に4桁の唯一のシーケンスであることを前提としています。特定のロケールを理解し、他の解析手法を使用することもできます。例えば、標準的な米国/英国は、年を最後に置く(例えば、、 "Tue Mar 04 19:20:17 MST 2014")、を.toString()に使用することができます。

/** 
* Obtains the year by converting the date .toString() and 
* finding the year by a regular expression; works by assuming that 
* no matter what the locale, only the year will have 4 digits 
*/ 
public static String getYearByRegEx(Date dte) throws IllegalArgumentException 
{ 
    String year = ""; 

    if (dte == null) { 
     throw new IllegalArgumentException("Null date!"); 
    } 

    // match only a 4 digit year 
    Pattern yearPat = Pattern.compile("^.*([\\d]{4}).*$"); 

    // convert the date to its String representation; could pass 
    // this directly, but I prefer the intermediary variable for 
    // potential debugging 
    String localDate = dte.toString(); 

    // obtain a matcher, and then see if we have the expected value 
    Matcher match = yearPat.matcher(localDate); 
    if (match.matches() && match.groupCount() == 1) { 
     year = match.group(1); 
    } 

    return year; 
} 


/** 
* Constructs a Calendar object, and then obtains the year 
* by using the Calendar.get(...) method for the year. 
*/ 
public static String getYearFromCalendar(Date dte) throws IllegalArgumentException 
{ 
    String year = ""; 

    if (dte == null) { 
     throw new IllegalArgumentException("Null date!"); 
    } 

    // get a Calendar 
    Calendar cal = Calendar.getInstance(); 

    // set the Calendar to the specific date; the reason why 
    // Calendar is deprecated is this mutability 
    cal.setTime(dte); 

    // get the year using the .get method, and convert to a String 
    year = String.valueOf(cal.get(Calendar.YEAR)); 

    return year; 
} 


/** 
* Uses the SimpleDateFormat with a format for only a year. 
*/ 
public static String getYearByFormatting(Date dte) 
     throws IllegalArgumentException 
{ 
    String year = ""; 

    if (dte == null) { 
     throw new IllegalArgumentException("Null date!"); 
    } 

    // set a format only for the year 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); 

    // format the date; the result is the year 
    year = sdf.format(dte); 

    return year;   
} 


public static void main(String[] args) 
{ 
    Calendar cal = Calendar.getInstance(); 
    cal.set(2014, 
      Calendar.MARCH, 
      04); 

    Date dte = cal.getTime(); 

    System.out.println("byRegex: "+ getYearByRegEx(dte)); 
    System.out.println("from Calendar: "+ getYearFromCalendar(dte)); 
    System.out.println("from format: " + getYearByFormatting(dte)); 
} 

3つのアプローチはすべて、テスト入力に基づいて予想される出力を返します。

+0

これはまさに私が探していたものです! getYearFromCalendar()は、使用するのに最も適した方法のようです。ありがとう! –

2

この

日付=新しいDate()を試してみてください。 LocalDate localDate = date.toInstant().Zone(ZoneId.systemDefault())。toLocalDate(); int year = localDate.getYear();ここで

はあなたから今年取得したいされている日付と新しい日付を代入

Calendar calendar = Calendar.getInstance(); 
calendar.setTime(new Date()); 
System.out.println(calendar.get(Calendar.YEAR)); 

を固定しています。

+1

OPはJava 8の新しいクラスを使用することは想定されていません。 – Keppil

+0

マルチタスキングされているので、それを見ている必要があります。 –

+0

待ち、別のテストクラスで私は次のことがあります。Calendar d = Calendar.getInstance(); d.set(2015、02、19); LicenceNumber l =新しいLicenceNumber(andrew、d.getTime());意味私はすでに日付オブジェクトをLicenceNumberに渡しました。 –

関連する問題