、LocalDate
とDateTimeFormatter
使用します:あなたは、以前のバージョンをサポートする必要がある場合
String startDateString = "16/02/2006";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/uuuu");
LocalDate localDate = LocalDate.parse(startDateString, dateTimeFormatter);
if (localDate.getYear() < 2006) {
// code here
}
を
DateFormat df = new SimpleDateFormat("yyyy");
Date yearOnReport = df.parse(startDateString);
Date threshold = df.parse("2006")
if (yearOnReport<threshold){
...\\Do some stuff
}
は、このであるべきJava、Calendar
とSimpleDateFormat
:
String startDateString = "16/02/2006";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = simpleDateFormat.parse(startDateString);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if (calendar.get(Calendar.YEAR) < 2006) {
// code here
}
のJava 7では、あなたはThreeTen projectから新しいjava.time
APIのバックポートを取得することにより、それをJavaの8道を行うことができます。
利点:コードは、代わりのJava 8に
余分なライブラリを必要とせずに、Javaの8にアップグレードする際に、後で、仕事Joda-Timeを追加し、そのLocalDate
とDateTimeFormat
使用します。
String startDateString = "16/02/2006";
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
LocalDate localDate = formatter.parseLocalDate(startDateString);
if (localDate.getYear() < 2006) {
// code here
}
しきい値が正しい単語ではありません私は、閾値は<=のように包括的であるためだと思います。 <のように排他的なので、代わりにtooLateを使用してください。 –
Java 8を使用していますか?もしそうなら、Jodaから派生した 'java.time'クラスを使うべきです。 –