質問編集履歴
2
コードの変更しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,6 +28,8 @@
|
|
28
28
|
|
29
29
|
```java
|
30
30
|
|
31
|
+
import java.util.ArrayList;
|
32
|
+
|
31
33
|
import java.util.Calendar;
|
32
34
|
|
33
35
|
|
@@ -40,39 +42,49 @@
|
|
40
42
|
|
41
43
|
// TODO 自動生成されたメソッド・スタブ
|
42
44
|
|
43
|
-
|
45
|
+
|
44
46
|
|
45
47
|
String fromym = "201501";
|
46
48
|
|
47
49
|
String toym = "201601";
|
48
50
|
|
49
|
-
|
51
|
+
ArrayList<String> yms = new ArrayList<String>(10);
|
50
52
|
|
51
53
|
Calendar calendar = Calendar.getInstance();
|
52
54
|
|
53
|
-
calendar.set(Integer.valueOf(fromym.substring(0,4)), Integer.valueOf(fromym.substring(4)), 1);
|
55
|
+
calendar.set(Integer.valueOf(fromym.substring(0, 4)), Integer.valueOf(fromym.substring(4)) - 1, 1);
|
54
56
|
|
55
|
-
|
56
57
|
|
57
|
-
//最初の年月の確認
|
58
58
|
|
59
|
-
|
59
|
+
// 最初の年月の確認
|
60
60
|
|
61
|
-
|
61
|
+
System.out.println(String.valueOf(calendar.get(Calendar.YEAR))
|
62
62
|
|
63
|
-
|
63
|
+
+ String.format("%02d", (calendar.get(Calendar.MONTH) + 1)));
|
64
64
|
|
65
|
+
|
66
|
+
|
67
|
+
while (!toym.equals(String.valueOf(calendar.get(Calendar.YEAR))
|
68
|
+
|
69
|
+
+ String.format("%02d", (calendar.get(Calendar.MONTH) + 1)))) {
|
70
|
+
|
65
|
-
//一月増やす
|
71
|
+
// 一月増やす
|
66
72
|
|
67
73
|
calendar.add(Calendar.MONTH, 1);
|
68
74
|
|
75
|
+
yms.add(String.valueOf(calendar.get(Calendar.YEAR))
|
76
|
+
|
77
|
+
+ String.format("%02d", (calendar.get(Calendar.MONTH) + 1)));
|
78
|
+
|
69
79
|
}
|
70
80
|
|
71
|
-
//
|
81
|
+
// 出力して確認
|
72
82
|
|
73
|
-
|
83
|
+
for (String ym : yms) {
|
74
84
|
|
75
|
-
|
85
|
+
System.out.println(ym);
|
86
|
+
|
87
|
+
}
|
76
88
|
|
77
89
|
}
|
78
90
|
|
1
コードの追加しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -22,4 +22,68 @@
|
|
22
22
|
|
23
23
|
カレンダークラスを使用してListを使う方法はわかりますが
|
24
24
|
|
25
|
-
どうしても処理が
|
25
|
+
どうしても処理が難読化してしまうためスマートな方法はありますでしょうか?
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```java
|
30
|
+
|
31
|
+
import java.util.Calendar;
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
public class Test3 {
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
public static void main(String[] args) {
|
40
|
+
|
41
|
+
// TODO 自動生成されたメソッド・スタブ
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
String fromym = "201501";
|
46
|
+
|
47
|
+
String toym = "201601";
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
Calendar calendar = Calendar.getInstance();
|
52
|
+
|
53
|
+
calendar.set(Integer.valueOf(fromym.substring(0,4)), Integer.valueOf(fromym.substring(4)), 1);
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
//最初の年月の確認
|
58
|
+
|
59
|
+
System.out.println(String.valueOf(calendar.get(Calendar.YEAR)) + String.format("%02d", (calendar.get(Calendar.MONTH))));
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
while (!toym.equals(String.valueOf(calendar.get(Calendar.YEAR)) + String.format("%02d", (calendar.get(Calendar.MONTH))))) {
|
64
|
+
|
65
|
+
//一月増やす
|
66
|
+
|
67
|
+
calendar.add(Calendar.MONTH, 1);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
//最後の年月の確認
|
72
|
+
|
73
|
+
System.out.println(String.valueOf(calendar.get(Calendar.YEAR)) + String.format("%02d", (calendar.get(Calendar.MONTH))));
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
このようになり、かなり分かりづらくなってしまうのでもう少しわかりやすくしたいのです。
|