LocalDateをGregorianCalendarに変換したい。ご意見をお聞かせください。 私のアプリケーションでは、GregorianCalendarを使用して日付を処理しています。しかし、私はjoda LocalDateを返す型として持っています。これは、Webサービスの1つを呼び出したときです。 今、月の値の表示が異なるため、LocalDateをGregorianCalendarに変換したいと考えています。以下のサンプルコードをご覧ください。 LocalDateは10月に10を表示しますが、GregorianCalendarでは10月に9が表示され、インデックスは0から始まります。 LocalDateをGregorianCalendarに変換する方法をお勧めします。LocalDateをJavaのGregorianCalendarに変換する
import java.util.GregorianCalendar;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
//sample code below
LocalDate localDateObj = new LocalDate();
System.out.println("Month value using LocalDate :" + localDateObj.getMonthOfYear());
System.out.println("Date using LocalDate : " + localDateObj);
//converting LocalDate to DateTime
DateTime dateTimeObj = localDateObj.toDateTimeAtStartOfDay();
System.out.println("DateTime month " + dateTimeObj.getMonthOfYear());
System.out.println("Date using DateTime : " + dateTimeObj);
//GregorianCalendar
GregorianCalendar gc = new GregorianCalendar();
System.out.println("Month value using GregorianCalendar:" + gc.OCTOBER);
System.out.println("GC :" + gc.getTime());
出力:
Month value using LocalDate :10
Date using LocalDate : 2016-10-13
DateTime month 10
Date using DateTime : 2016-10-13T00:00:00.000-04:00
Month value using GregorianCalendar:9
GC :Thu Oct 13 16:14:43 EDT 2016
java.util.GregorianCalendarにorg.joda.time.LocalDateを変換する方法をアドバイスやこれを解決するための任意のより良い方法があります。 GregorianCalendarを使用しているロジックを変更できないので、joda LocalDateを使用してGregorianCalendarとして月を表示するロジックを変更したいとします(例:10月は0から11のインデックス開始として9を表示します)。
'gregorianCalendar.setDate(localDate.toDate())'を使用できますか? – msandiford
@msandiford - GregorianCalendarにはsetDate(..)メソッドがありません。そのことはできません。 – joan
私の申し訳ありませんが、最後に入力してください。私は 'gregorianCalendar.setTime(localDate.toDate())'を意味しました。名前にもかかわらず、これは実際に日付を設定します。 – msandiford