2009-06-29 17 views
6

Joda時間に整数の日数を追加する簡単なユーティリティメソッドを作成しようとしています。instant。ここに私の最初の刺し傷があります。JodaTimeインスタントに日数を追加する

/** 
* Adds a number of days specified to the instant in time specified. 
* 
* @param instant - the date to be added to 
* @param numberOfDaysToAdd - the number of days to be added to the instant specified 
* @return an instant that has been incremented by the number of days specified 
*/ 
public static Instant addNumberOfDaysToInstant(final Instant instant, final int numberOfDaysToAdd) { 
    Days days = Days.days(numberOfDaysToAdd); 
    Interval interval = new Interval(instant, days); 
    return interval.getEnd().toInstant(); 
} 

これは、追加日数がBST/GMT境界を越えてあなたを取るとき、あなたが例を考えるときを除いてほとんどの部分は正常に動作します。ここに小さな例があります。

public class DateAddTest { 

/** * ゾーンは、入力と出力に使用する */ プライベート静的最終DateTimeZoneをZONE = DateTimeZone.forId( "ヨーロッパ/ロンドン");

/** 
* Formatter used to translate Instant objects to & from strings. 
*/ 
private static final DateTimeFormatter FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT).withZone(ZONE); 


/** 
* Date format to be used 
*/ 
private static final String DATE_FORMAT = "dd/MM/yyyy"; 


public static void main(String[] args) { 

DateTime dateTime = FORMATTER.parseDateTime("24/10/2009"); 
Instant toAdd = dateTime.toInstant(); 
Instant answer = JodaTimeUtils.addNumberOfDaysToInstant(toAdd, 2); 

System.out.println(answer.toString(FORMATTER)); //25/10/2009 
} 

}

私は間隔がACOUNTにそれはBSTの境界を越えたことになりませんので、この問題はあると思います。これを実装するためのより良い方法のアイデアは高く評価されます。

+1

インスタント引数を作成する時間帯はどのような予定ですか?一貫性がありますか? Jonによって提案された元の修正を利用できないため、何らかの理由でその値が考慮されなければ、計算は無意味です。 – laz

答えて

0

これが選ばれたソリューションです。

/** 
* Zone to use for input and output 
*/ 
private static final DateTimeZone ZONE = DateTimeZone.forId("Europe/London"); 

/** 
* Adds a number of days specified to the instant in time specified. 
* 
* @param instant - the date to be added to 
* @param numberOfDaysToAdd - the number of days to be added to the instant specified 
* @return an instant that has been incremented by the number of days specified 
*/ 
public static Instant addNumberOfDaysToInstant(final Instant instant, final int numberOfDaysToAdd) { 
    return instant.toDateTime(ZONE).withFieldAdded(DurationFieldType.days(), numberOfDaysToAdd).toInstant(); 
} 
+4

これは簡単です。 return instant.toDateTime(ZONE).plusDays(numberOfDaysToAdd).toInstant(); – JodaStephen

7

の日付を処理する場合は、インスタントを使用しないでください。私はそれが正しく瞬時に48時間を追加していると思う。

代わりにLocalDateを使用し、次にplusDaysメソッドを使用します。

時刻が指定された瞬間のn日後に発生する瞬間を同じ時刻に知りたい場合は、瞬間をLocalDateLocalTimeに分割してLocalDateを再構築してください、またはLocalDateTimeがあなたの望むことを実行しているかどうかを確認してください)、元の時刻が新しい日に2回発生した場合や、まったく発生しない場合は、

編集:さて、あなたは即座に作業する必要があります。それは元のタイムゾーンになければなりませんか? UTCを使用できますか?それはDSTの問題を取り除くでしょう。そうでない場合は、あいまいさや不存在の場合(例えば、各遷移の前に12時30分に)、何をしたいですか?

+0

LocalDateはこの場合は瞬時よりも賢明ですが、他の制約のために変更することはできません。 –

2

あなたのコードの残りの部分と仮定すると:

public static void main(String[] args) { 

    DateTime dateTime = FORMATTER.parseDateTime("24/10/2009"); 
    Instant pInstant = dateTime.withFieldAdded(DurationFieldType.days(),2).toInstant(); 
    System.out.println("24/10/2009 + 2 Days = " + pInstant.toString(FORMATTER)); 
} 
関連する問題