站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题求一个小的java程序

求一个小的java程序

摘要: 求一个小的java程序


任意录入两个年月的串,将两个年月的差输入到一个二维数组中 String a[][]
例如:输入 200406 200609
返回的二维数组中包括:
a[0][0]=2004
a[0][1]=6
a[1][0]=2004
a[1][1]=7
a[2][0]=2004
a[2][1]=8
......
a[24][0]=2006
a[24][1]=7
a[25][0]=2006
a[25][1]=8
a[26][0]=2006
a[26][1]=9
请问谁有又简洁又有效率的算法。谢谢!




想了一个算法,可能并不简洁,也不高效,但感觉实际应用中两个日期间不可能差上千上万个月吧。
public class Test
{
public static void main(String[] args)
{
int a[][] = DividDate.divid("200406","200609");
if(a != null)
for(int i=0;i{
System.out.println("a[" + i + "][0]=" + a[i][0]);
System.out.println("a[" + i + "][1]=" + a[i][1]);
}
else
System.out.println("error");
}

}

class DividDate
{
public static int[][] divid(String str1,String str2)
{
int results[][];
int d1;
int d2;
int year1;
int year2;
int month1;
int month2;
int k=0;
year1 = Integer.parseInt(str1.substring(0,4));
year2 = Integer.parseInt(str2.substring(0,4));
month1 = Integer.parseInt(str1.substring(4,6));
month2 = Integer.parseInt(str2.substring(4,6));
if(year2 < year1)
{
return null;
}
else if(year2 == year2 && month2 <= month1)
{
return null;
}
else
{
d1 = year2 - year1 + 1;
if(d1 > 1)
{
d2 = 12 - month1 + 1 + month2 + (d1>2?(d1-2)*12:0);
results = new int[d2][2];
for(int i=0;i<=12-month1;i++)
{
results[i][0] = year1;
results[i][1] = month1 + i;
k = i;
}
for(int i=1;i<=d1-2;i++)
{
for(int j=1;j<=12;j++)
{
k = k + 1;
results[k][0] = year1 + i;
results[k][1] = j;
}
}
for(int i=1;i<=month2;i++)
{
k = k + 1;
results[k][0] = year2;
results[k][1] = i;
}
}
else
{
d2 = month2 - month1 + 1;
results = new int[d2][2];
for(int i=0;i{
results[i][0] = year1;
results[i][1] = month1 + i;
}
}

return results;

}
}
}


↑返回目录
前一篇: div的id
后一篇: 我想将一个字符串中除了数字、英文字符、中文字符以外的所有符号全部删除,怎么办?