存储在数组中的日期如何将所有jan月份日期分别分组在不同的数组中
String s[]={"2016-05-8","2016-01-18","2016-03-23","2016-01-15","2016-02-12","2016-03-7"};
SimpleDateFormat newformat = new SimpleDateFormat("dd-MM-yyyy");
String jan[]=new String[10];
String march[]=new String[10];
for(int i=0;i<s.length;i++)
{
int month=Integer.parseInt(date.split("-")[1]);
switch(month)
{
case 01:
System.out.println("JAN month");
String[] jan=new String[10];
for(int k=0;k<3;k++)
{
jan[k]=date;
}
System.out.println("dates in jan month "+jan[0]);
System.out.println("dates in jan month "+jan[1]);
break;
case 02:
System.out.println("-------------------------------------------------");
System.out.println("FEB month");
String[] feb=new String[10];
for(int k=0;k<3;k++)
{
feb[k]=date;
}
System.out.println("dates in jan month "+feb[0]);
break;
case 03:
System.out.println("march month");
break;
case 04:
System.out.println("apr month");
break;
case 05:
System.out.println("-------------------------------------------------");
System.out.println("MAY month");
String[] may=new String[10];
for(int k=0;k<3;k++)
{
may[k]=date;
}
System.out.println("dates in MAY month "+may[0]);
break;
}
}
答案 0 :(得分:0)
String[] months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
String[] s = { "2016-05-8", "2016-01-18", "2016-03-23", "2016-01-15", "2016-02-12", "2016-03-7" };
Map<String, List<String>> datesMap = new HashMap<String, List<String>>(s.length);
for (String date : s) {
String month = date.substring(date.indexOf("-") + 1, date.lastIndexOf("-"));
List<String> strings = datesMap.get(month);
if (strings == null) {
strings = new ArrayList<String>();
datesMap.put(month, strings);
}
strings.add(date);
}
Set<String> month = datesMap.keySet();
for (String date : month) {
List<String> list = datesMap.get(date);
if (list != null && !list.isEmpty()) {
System.out.println("dates in " + months[Integer.parseInt(date) - 1] + " month:" + list.toString());
}
}
欢迎你。我的名字是雷锋