2017-03-08 7 views
0

年月日を別途に設定しましたJComboBoxes月JComboBoxにinteger月の値ではなく月の名前を表示するように自分自身がrendererです。月がどこから始まるのか0 (0-January....11-December)JComboBoxの年、月、日の整数値を日付型に連結または書式設定

私の問題は、年、月、日を一緒にしてDateデータ型に割り当てることです。

私は String変数

int startYear = Integer.parseInt(jcmbFirstSemStartDateYear.getSelectedItem().toString()); 
int startMonth = Integer.parseInt(jcmbFirstSemStartDateMonth.getSelectedItem().toString()); 
int startDay = Integer.parseInt(jcmbFirstSemStartDateDay.getSelectedItem().toString()); 

String startDate = startYear+"-"+startMonth+"-"+startDay; //not sure if this okay 
Date completeStartDate = java.sql.Date.valueOf(startDate); 
JOptionPane.showMessageDialog(null,completeStartDate); 

hasConflict(completeStartDate, completeEndDate); //method to check conflict 

に一緒に年、月、日の連結を試みたが、私はcompleteStartDateの値を印刷したとき、それは01

enter image description here

2月の月を表示

2月の代わりに1月と読み替えて、競合をチェックする月を抽出すると心配です。

public static boolean hasConflict(Date aStartDate, Date aEndDate){ 
     boolean hasConflict = false; 
     Calendar calStart = Calendar.getInstance(); 
     calStart.setTime(aStartDate); 
     int startYear = calStart.get(Calendar.YEAR); 
     int startMonth = calStart.get(Calendar.MONTH); //extract month 
     int startDay = calStart.get(Calendar.DAY_OF_MONTH); 
     // ... if else conditions to compare startDate with endDate 
     return hasConflict; 
    } 

後で、これらの日付をmysqlデータベースに保存します。

どのような考えやアドバイスですか?

ありがとうございます。

+0

フォーマットに基づいて 'String'表現を作成して解析することもできますし、[' LocalDate.of(year、month、dayOfMonth) '](https://docs.oracle.com/javase)を使用することもできます。 /8/docs/api/java/time/LocalDate.html#of-int-int-int-) – MadProgrammer

答えて

2

あなたはおそらくあなたがString表現を作成し、

String dateValue = startYear + "/" + startMonth + "/" + startDay; 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); 
Date data = sdf.parse(dateValue); 

をそれを解析でき一気にLocalDate.of(year, month, dayOfMonth)

LocalDate date = LocalDate.of(startYear, startMonth, startDay); 

を使用しかしLocalDateのシンプルさを与えなければならない可能ならば、私はそれのために行くだろう...さらに、Java 8以上を使用している場合は、実際にjava.util.Dateを使用するべきではありませんし、そうでない場合でも、JodaTimeはとにかくjava.util.Date以上と考えていたでしょう

+0

完全に一致します。彼らはあなたがJodaTimeよりも[Java SE 6と7へのJava SE 8日時クラスのThreeTen-Backport](http://www.threeten.org/threetenbp/)を好むべきだと言います。私は、あなたがJava 8に後で変更した場合、これがあなたに互換性を与えることを期待しています。 –

関連する問題