List<PerformanceDataValues> dataList = new ArrayList<>();
for (Entry<String, String> entry : treemap.entrySet()) {
PerformanceDataValues data = new PerformanceDataValues();
data.setTime(entry.getKey());
String value = entry.getValue();
String computedValue = applyFormula(value, wsdlInput.getFormulae());
data.setValue(computedValue != null ? computedValue : value);
dataList.add(data);
}
package com.srini;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MapExamples4 {
public static void main(String[] args) throws ParseException {
Map<String, String> map = new TreeMap<>();
map.put("2016-01-14 13:30:00", "253.000");
map.put("2016-01-14 12:30:00", "53.000");
map.put("2016-01-15 10:30:00", "353.000");
map.put("2016-01-15 01:30:00", "563.000");
map.put("2016-01-16 02:30:00", "693.000");
map.put("2016-01-16 03:30:00", "283.000");
map.put("2016-01-15 09:30:00", "273.000");
Set<String> dateval = map.keySet();
System.out.println(dateval);
List<Date> list = new ArrayList<>();
for (String strDate : dateval) {
list.add(convertStringToDate(strDate));
}
//int days = 2;//To Get Last two days data.
int days = 1; //To Get current date values
Map<String, String> filteredMap = new LinkedHashMap<String, String>();
if (days == 2) {
filteredMap = getPreviousDateMap(map, list, filteredMap);
} else if (days == 1) {
filteredMap = getCurrentDateMap(map, list, filteredMap);
}
}
private static Map<String, String> getCurrentDateMap(Map<String, String> map, List<Date> list,
Map<String, String> filteredMap) throws ParseException {
Date d2 = getPreviousDay(1);
System.out.println("d2 value is" + d2);
List<Date> afterDateList = new ArrayList<Date>();
for (Date dateList : list) {
if (d2.before(dateList)) {
afterDateList.add(dateList);
}
}
System.out.println("current date is " + afterDateList);
getFilteredMap(map,filteredMap,afterDateList);
return filteredMap;
}
private static Map<String, String> getPreviousDateMap(
Map<String, String> map, List<Date> list,
Map<String, String> filteredMap) throws ParseException {
Date d1 = getPreviousDay(2);
System.out.println("d1 value is" + d1);
List<Date> currentDateList = new ArrayList<Date>();
for (Date dateList : list) {
if (d1.before(dateList)) {
currentDateList.add(dateList);
}
}
System.out.println("before date is " + currentDateList);
getFilteredMap(map, filteredMap, currentDateList);
return filteredMap;
}
private static void getFilteredMap(Map<String, String> map,
Map<String, String> filteredMap, List<Date> currentDateList)
throws ParseException {
for (Date date : currentDateList) {
filteredMap.put(convertDateToString(date),
map.get(convertDateToString(date)));
}
System.out.println("FILTERED MAP IS" + filteredMap);
}
public static Date convertStringToDate(String strDate)
throws ParseException {
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df2.parse(strDate);
}
public static String convertDateToString(Date strDate)
throws ParseException {
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df2.format(strDate);
}
public static Date getPreviousDay(int days) {
Calendar cal = Calendar.getInstance(); // now
cal.add(Calendar.DATE, -days); // Minus one day
return cal.getTime();
}
}
Thanks,
if possible trim the codeI would first like you to make it work correctly. Once that is the case, then it is the right time to trim.
System.out.println("current date is " + afterDateList);
andSystem.out.println("before date is " + currentDateList);
are definitely wrong since afterDateList and currentDateList are ... well Lists of Date's, not one Date. for (Date date : currentDateList) {
filteredMap.put(convertDateToString(date),
map.get(convertDateToString(date)));
}
as for (Date date : currentDateList) {
String dateAsString = convertDateToString(date);
filteredMap.put(dateAsString, map.get(dateAsString));
}
Date d2 = getPreviousDay(1);
while some lines further you call the result of getPreviousDay(2) "d1"Date d1 = getPreviousDay(2);
Give your variables clear names; names that indicate what is in it.Calendar cal = Calendar.getInstance(); // now
This is really now, including hours, minutes, seconds, milliseconds. public static Date getPreviousDay(int days) {
Calendar cal = Calendar.getInstance(); // now
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0); // today @ midnight
cal.add(Calendar.DATE, -days); // Minus one day
return cal.getTime();
}
the above line will print array of listI know. But I find it very strange to read a sentence like this:
if possible trim the codeI think your code is just fine as it is now.