js代码:
var time = ["2010-01","2010-03","2010-09","2012-02","2013-12"];
var usage = [100,80,130,30,120];
function updateTU(time, usage){
var temp1,temp2;temp1 = time[0];
temp2 = time[time.length-1];
var ymd1 = temp1.split("-");
var ymd2 = temp2.split("-");
var y1 = parseInt(ymd1[0]);
var m1 = parseInt(ymd1[1]);
var y2 = parseInt(ymd2[0]);
var m2 = parseInt(ymd2[1]);
var y3 = (y2 - y1)*12 + (m2 - m1);
newTime = new Array(y3+1);
newTime[0] = {value:1,text:time[0]};
newTime[y3] = {value:(y3+1),text:time[time.length-1]};
newUsage = new Array(y3+1);
newUsage[0] = usage[0];
newUsage[y3] = usage[usage.length-1];
var tempUsage = newUsage[0];
var mon = "";
var month = m1;
for (var j = 1; j < y3; j++) {
if((month+1) > 12){
y1 = y1+1;
month = 1;
}else {
month = month+1;
}
mon = month > 9 ? month+"" : "0"+month;
var tempTime = y1+"-"+mon;
for (var k = 0; k < time.length; k++) {
if(time[k].contains(tempTime)){
tempUsage = usage[k];
newTime[j] = {value:(j+1),text:tempTime};
newUsage[j] = tempUsage;
break;
}else {
newTime[j] = {value:(j+1),text:''};
newUsage[j] = tempUsage;
}
}
}
}
java代码:
public static void main(String[] args) { String time[] = {"2010-01","2012-02","2013-01","2013-12"}; int usage[] = {100,80,90,120}; updateTU(time, usage); } public static void updateTU(String time[], int usage[]){ String temp1,temp2; temp1 = time[0]; temp2 = time[time.length-1]; String ymd1[] = temp1.split("-"); String ymd2[] = temp2.split("-"); int y1 = Integer.parseInt(ymd1[0]); int m1 = Integer.parseInt(ymd1[1]); int y2 = Integer.parseInt(ymd2[0]); int m2 = Integer.parseInt(ymd2[1]); int y3 = (y2 - y1)*12 + (m2 - m1); System.out.println("count:"+y3); String newTime[] = new String[y3+1]; newTime[0] = "{value:"+1+",text: '"+time[0]+"'}"; newTime[y3] = "{value:"+(y3+1)+",text: '"+time[time.length-1]+"'}"; int newUsage[] = new int[y3+1]; newUsage[0] = usage[0]; newUsage[y3] = usage[usage.length-1]; int tempUsage = newUsage[0]; String mon = ""; int month = m1; for (int j = 1; j < y3; j++) { if((month+1) > 12){ y1 = y1+1; month = 1; }else { month = month+1; } mon = month > 9 ? month+"" : "0"+month; String tempTime = y1+"-"+mon; for (int k = 0; k < time.length; k++) { if(time[k].equals(tempTime)){ tempUsage = usage[k]; newTime[j] = "{value:"+(j+1)+",text: '"+tempTime+"'}"; newUsage[j] = tempUsage; break; }else { newTime[j] = "{value:"+(j+1)+",text: ''}"; newUsage[j] = tempUsage; } } } for (int i = 0; i < newTime.length; i++) { System.out.print(newTime[i]+" "); System.out.print(newUsage[i]); System.out.println(); } }