Created
August 15, 2012 05:39
-
-
Save komiya-atsushi/3356476 to your computer and use it in GitHub Desktop.
Date オブジェクトの任意のフィールドをインクリメントして、for-each ループで繰り返し処理できるようにするクラス。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package biz.k11i.util; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
/** | |
* Date オブジェクトの任意のフィールドをインクリメントしながら繰り返す Iterator の実装です。 | |
* <p> | |
* インクリメントするフィールドとして、以下の値の指定を想定しています。 | |
* <ul> | |
* <li>Calendar.YEAR</li> | |
* <li>Calendar.MONTH</li> | |
* <li>Calendar.DATE</li> | |
* <li>Calendar.HOUR</li> | |
* <li>Calendar.MINUTE</li> | |
* <li>Calendar.SECOND</li> | |
* <li>Calendar.MILLISECOND</li> | |
* </ul> | |
* </p> | |
* | |
* @author komiya | |
*/ | |
public class DateIterator implements Iterator<Date> { | |
/** 繰り返し中の現在の値 */ | |
private Date current; | |
/** 終了値となる Date オブジェクト。この値を超えない範囲で繰り返し処理が行われます。 */ | |
private Date to; | |
/** 繰り返しの際に +1 される Date オブジェクトの項目(年、月、日、時、分、秒、ミリ秒) */ | |
private int incrementalField; | |
/** 次の next() メソッド呼び出しで返却すべきオブジェクト */ | |
private Date nextObject; | |
/** | |
* 初期値、終了値、インクリメントするフィールドを指定して、DateIterator オブジェクトを生成します。 | |
* | |
* @param from | |
* 初期値 | |
* @param to | |
* 終了値 (含まれません) | |
* @param incrementalField | |
* インクリメントするフィールド。Calendar クラスの YEAR などの定数を指定します。 | |
*/ | |
public DateIterator(Date from, Date to, int incrementalField) { | |
this.current = from; | |
this.to = to; | |
this.incrementalField = incrementalField; | |
} | |
@Override | |
public boolean hasNext() { | |
if (nextObject != null) { | |
return true; | |
} | |
if (!current.before(to)) { | |
return false; | |
} | |
nextObject = current; | |
Calendar cal = Calendar.getInstance(); | |
cal.setTime(current); | |
cal.add(incrementalField, 1); | |
current = cal.getTime(); | |
return true; | |
} | |
@Override | |
public Date next() { | |
if (!hasNext()) { | |
throw new NoSuchElementException(); | |
} | |
Date result = nextObject; | |
nextObject = null; | |
return result; | |
} | |
@Override | |
public void remove() { | |
throw new UnsupportedOperationException(); | |
} | |
/** | |
* DateIterator を返却する Iterable オブジェクトを生成し、返却します。 | |
* | |
* @param from | |
* 初期値 | |
* @param to | |
* 終了値 (含まれません) | |
* @param incrementalField | |
* インクリメントするフィールド。Calendar クラスの YEAR などの定数を指定します。 | |
* @return | |
*/ | |
public static Iterable<Date> createIterable(Date from, Date to, | |
int incrementalField) { | |
final Date tempFrom = from; | |
final Date tempTo = to; | |
final int tempIncrementalField = incrementalField; | |
return new Iterable<Date>() { | |
@Override | |
public Iterator<Date> iterator() { | |
return new DateIterator(tempFrom, tempTo, tempIncrementalField); | |
} | |
}; | |
} | |
/** | |
* DateIterator を利用するデモプログラム。 | |
* | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
Date to = new Date(); | |
Date from = new Date(to.getTime() - (24 * 60 * 60 * 1000)); | |
System.out.println(String.format("from : %s", from.toString())); | |
System.out.println(String.format("to : %s", to.toString())); | |
for (Date date : DateIterator.createIterable(from, to, Calendar.HOUR)) { | |
System.out.println(date.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment