,如果你可以讀取數據到豆然後排序& &比較,最後一個是設置 一天,我的代碼:
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author heyunxia ([email protected])
* @version 1.0
* @since 2016-01-15 上午10:30
*/
public class SaS {
public static final SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy");
public static void main(String[] args) throws ParseException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
List<DataSet> list = new ArrayList<>();
list.add(new DataSet(sdf.parse("1/1/2013"), 1)); //0
list.add(new DataSet(sdf.parse("3/2/2013"), 1)); //60
list.add(new DataSet(sdf.parse("4/5/2015"), 1)); //764
list.add(new DataSet(sdf.parse("4/5/2003"), 2)); //0
list.add(new DataSet(sdf.parse("6/7/2003"), 2)); //63
list.add(new DataSet(sdf.parse("8/1/2004"), 2)); //421
list.add(new DataSet(sdf.parse("1/1/2005"), 2)); //153
list.add(new DataSet(sdf.parse("4/5/2003"), 3)); //0
list.add(new DataSet(sdf.parse("6/5/2003"), 3)); //60
Map<Integer, List<DataSet>> map = list2GroupedMap(list, "store");
System.out.println(map);
Set<Integer> keySet = map.keySet();
Iterator<Integer> iterator = keySet.iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
List<DataSet> dataSetList = map.get(key);
DataSet[] dataSets = new DataSet[dataSetList.size()];
dataSetList.toArray(dataSets);
Arrays.sort(dataSets);
setDays(dataSets);
}
System.out.println(map);
}
private static final int DEFAULT_DAYS = 0;
//calculate set DataSet property day
private static void setDays(DataSet[] dataSets) {
int i = 0;
DataSet preDataSet = dataSets[i];
preDataSet.setDay(DEFAULT_DAYS); // first one default was 0
DataSet curDataSet;
for (i = 1; i < dataSets.length; i++) {//skip first one
curDataSet = dataSets[i];
curDataSet.setDay(daysBetween(preDataSet.getDate(), curDataSet.getDate()));
preDataSet = curDataSet;
}
}
public static int daysBetween(Date preData, Date curData) {
Calendar cal = Calendar.getInstance();
cal.setTime(preData);
long preTime = cal.getTimeInMillis();
cal.setTime(curData);
long curTime = cal.getTimeInMillis();
long betweenDays = (curTime - preTime)/(1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(betweenDays));
}
private static <T> T getProperty(String propertyName, Object object) {
try {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, object.getClass());
Method method = pd.getReadMethod();
return (T) method.invoke(object);
} catch (Exception ex) {
throw new RuntimeException();
}
}
/**
*
*
* @param objList
* @param propertyName
* @param <E>
* @param <T>
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public static <E, T> Map<E, List<T>> list2GroupedMap(List<T> objList, String propertyName) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
if (objList == null || objList.size() == 0)
throw new IllegalArgumentException("invalid argument objList");
if (propertyName == null || "".equals(propertyName)) {
throw new IllegalArgumentException("invalid argument propertyName");
}
Map<E, List<T>> map = new ConcurrentHashMap<>();
for (int i = 0; i < objList.size(); i++) {
T obj = objList.get(i);
Object propertyValue = getProperty(propertyName, obj);
List<T> innerList = map.get(propertyValue);
if (innerList == null || innerList.size() == 0) {
innerList = new ArrayList<>();
}
innerList.add(obj);
map.put((E) propertyValue, innerList);
}
return map;
}
}
class DataSet implements Serializable, Comparable<DataSet> {
private Date date;
private Integer store;
private Integer day;
public DataSet() {
}
public DataSet(Date date, Integer store) {
this.date = date;
this.store = store;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getStore() {
return store;
}
public void setStore(Integer store) {
this.store = store;
}
public Integer getDay() {
return day;
}
public void setDay(Integer day) {
this.day = day;
}
@Override
public int compareTo(DataSet o) {
if (this.date.getTime() > o.date.getTime()) {
return 1;
}
return -1;
}
}