用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间
1
package
vivi.test;
2
3 import java.util.Calendar;
4
5 /**
6 *
7 * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串
8 * 1、得到当前日期 today()
9 * 2、得到当前月份月初 thisMonth()
10 * 3、得到当前月份月底 thisMonthEnd()
11 * 4、得到当前季度季初 thisSeason()
12 * 5、得到当前季度季末 thisSeasonEnd()
13 * 6、得到当前年份年初 thisYear()
14 * 7、得到当前年份年底 thisYearEnd()
15 * 8、判断输入年份是否为闰年 leapYear
16 *
17 * 注意事项: 日期格式为:xxxx-yy-zz (eg: 2007-12-05)
18 *
19 * 实例:
20 *
21 * @author pure
22 */
23 public class Test {
24
25 private int x; // 日期属性:年
26
27 private int y; // 日期属性:月
28
29 private int z; // 日期属性:日
30
31 private Calendar localTime; // 当前日期
32
33 public Test() {
34 localTime = Calendar.getInstance();
35 }
36 public static void main(String[] args){
37 Test test = new Test();
38 test.today();
39 }
40
41
42 /**
43 * 功能:得到当前日期 格式为:xxxx-yy-zz (eg: 2007-12-05)
44 * @return String
45 * @author pure
46 */
47 public String today() {
48 String strY = null ;
49 String strZ = null ;
50 x = localTime.get(Calendar.YEAR);
51 y = localTime.get(Calendar.MONTH) + 1 ;
52 z = localTime.get(Calendar.DATE);
53 strY = y >= 10 ? String.valueOf(y) : ( " 0 " + y);
54 strZ = z >= 10 ? String.valueOf(z) : ( " 0 " + z);
55 return x + " - " + strY + " - " + strZ;
56 }
57
58 /**
59 * 功能:得到当前月份月初 格式为:xxxx-yy-zz (eg: 2007-12-01)
60 * @return String
61 * @author pure
62 */
63 public String thisMonth() {
64 String strY = null ;
65 x = localTime.get(Calendar.YEAR);
66 y = localTime.get(Calendar.MONTH) + 1 ;
67 strY = y >= 10 ? String.valueOf(y) : ( " 0 " + y);
68 return x + " - " + strY + " -01 " ;
69 }
70
71 /**
72 * 功能:得到当前月份月底 格式为:xxxx-yy-zz (eg: 2007-12-31)
73 * @return String
74 * @author pure
75 */
76 public String thisMonthEnd() {
77 String strY = null ;
78 String strZ = null ;
79 boolean leap = false ;
80 x = localTime.get(Calendar.YEAR);
81 y = localTime.get(Calendar.MONTH) + 1 ;
82 if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12 ) {
83 strZ = " 31 " ;
84 }
85 if (y == 4 || y == 6 || y == 9 || y == 11 ) {
86 strZ = " 30 " ;
87 }
88 if (y == 2 ) {
89 leap = leapYear(x);
90 if (leap) {
91 strZ = " 29 " ;
92 }
93 else {
94 strZ = " 28 " ;
95 }
96 }
97 strY = y >= 10 ? String.valueOf(y) : ( " 0 " + y);
98 return x + " - " + strY + " - " + strZ;
99 }
100
101 /**
102 * 功能:得到当前季度季初 格式为:xxxx-yy-zz (eg: 2007-10-01)<br>
103 * @return String
104 * @author pure
105 */
106 public String thisSeason() {
107 String dateString = "" ;
108 x = localTime.get(Calendar.YEAR);
109 y = localTime.get(Calendar.MONTH) + 1 ;
110 if (y >= 1 && y <= 3 ) {
111 dateString = x + " - " + " 01 " + " - " + " 01 " ;
112 }
113 if (y >= 4 && y <= 6 ) {
114 dateString = x + " - " + " 04 " + " - " + " 01 " ;
115 }
116 if (y >= 7 && y <= 9 ) {
117 dateString = x + " - " + " 07 " + " - " + " 01 " ;
118 }
119 if (y >= 10 && y <= 12 ) {
120 dateString = x + " - " + " 10 " + " - " + " 01 " ;
121 }
122 return dateString;
123 }
124
125 /**
126 * 功能:得到当前季度季末 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>
127 * @return String
128 * @author pure
129 */
130 public String thisSeasonEnd() {
131 String dateString = "" ;
132 x = localTime.get(Calendar.YEAR);
133 y = localTime.get(Calendar.MONTH) + 1 ;
134 if (y >= 1 && y <= 3 ) {
135 dateString = x + " - " + " 03 " + " - " + " 31 " ;
136 }
137 if (y >= 4 && y <= 6 ) {
138 dateString = x + " - " + " 06 " + " - " + " 30 " ;
139 }
140 if (y >= 7 && y <= 9 ) {
141 dateString = x + " - " + " 09 " + " - " + " 30 " ;
142 }
143 if (y >= 10 && y <= 12 ) {
144 dateString = x + " - " + " 12 " + " - " + " 31 " ;
145 }
146 return dateString;
147 }
148
149 /**
150 * 功能:得到当前年份年初 格式为:xxxx-yy-zz (eg: 2007-01-01)<br>
151 * @return String
152 * @author pure
153 */
154 public String thisYear() {
155 x = localTime.get(Calendar.YEAR);
156 return x + " -01 " + " -01 " ;
157 }
158
159 /**
160 * 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>
161 * @return String
162 * @author pure
163 */
164 public String thisYearEnd() {
165 x = localTime.get(Calendar.YEAR);
166 return x + " -12 " + " -31 " ;
167 }
168
169 /**
170 * 功能:判断输入年份是否为闰年<br>
171 *
172 * @param year
173 * @return 是:true 否:false
174 * @author pure
175 */
176 public boolean leapYear( int year) {
177 boolean leap;
178 if (year % 4 == 0 ) {
179 if (year % 100 == 0 ) {
180 if (year % 400 == 0 ) leap = true ;
181 else leap = false ;
182 }
183 else leap = true ;
184 }
185 else leap = false ;
186 return leap;
187 }
188 }
189
2
3 import java.util.Calendar;
4
5 /**
6 *
7 * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串
8 * 1、得到当前日期 today()
9 * 2、得到当前月份月初 thisMonth()
10 * 3、得到当前月份月底 thisMonthEnd()
11 * 4、得到当前季度季初 thisSeason()
12 * 5、得到当前季度季末 thisSeasonEnd()
13 * 6、得到当前年份年初 thisYear()
14 * 7、得到当前年份年底 thisYearEnd()
15 * 8、判断输入年份是否为闰年 leapYear
16 *
17 * 注意事项: 日期格式为:xxxx-yy-zz (eg: 2007-12-05)
18 *
19 * 实例:
20 *
21 * @author pure
22 */
23 public class Test {
24
25 private int x; // 日期属性:年
26
27 private int y; // 日期属性:月
28
29 private int z; // 日期属性:日
30
31 private Calendar localTime; // 当前日期
32
33 public Test() {
34 localTime = Calendar.getInstance();
35 }
36 public static void main(String[] args){
37 Test test = new Test();
38 test.today();
39 }
40
41
42 /**
43 * 功能:得到当前日期 格式为:xxxx-yy-zz (eg: 2007-12-05)
44 * @return String
45 * @author pure
46 */
47 public String today() {
48 String strY = null ;
49 String strZ = null ;
50 x = localTime.get(Calendar.YEAR);
51 y = localTime.get(Calendar.MONTH) + 1 ;
52 z = localTime.get(Calendar.DATE);
53 strY = y >= 10 ? String.valueOf(y) : ( " 0 " + y);
54 strZ = z >= 10 ? String.valueOf(z) : ( " 0 " + z);
55 return x + " - " + strY + " - " + strZ;
56 }
57
58 /**
59 * 功能:得到当前月份月初 格式为:xxxx-yy-zz (eg: 2007-12-01)
60 * @return String
61 * @author pure
62 */
63 public String thisMonth() {
64 String strY = null ;
65 x = localTime.get(Calendar.YEAR);
66 y = localTime.get(Calendar.MONTH) + 1 ;
67 strY = y >= 10 ? String.valueOf(y) : ( " 0 " + y);
68 return x + " - " + strY + " -01 " ;
69 }
70
71 /**
72 * 功能:得到当前月份月底 格式为:xxxx-yy-zz (eg: 2007-12-31)
73 * @return String
74 * @author pure
75 */
76 public String thisMonthEnd() {
77 String strY = null ;
78 String strZ = null ;
79 boolean leap = false ;
80 x = localTime.get(Calendar.YEAR);
81 y = localTime.get(Calendar.MONTH) + 1 ;
82 if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12 ) {
83 strZ = " 31 " ;
84 }
85 if (y == 4 || y == 6 || y == 9 || y == 11 ) {
86 strZ = " 30 " ;
87 }
88 if (y == 2 ) {
89 leap = leapYear(x);
90 if (leap) {
91 strZ = " 29 " ;
92 }
93 else {
94 strZ = " 28 " ;
95 }
96 }
97 strY = y >= 10 ? String.valueOf(y) : ( " 0 " + y);
98 return x + " - " + strY + " - " + strZ;
99 }
100
101 /**
102 * 功能:得到当前季度季初 格式为:xxxx-yy-zz (eg: 2007-10-01)<br>
103 * @return String
104 * @author pure
105 */
106 public String thisSeason() {
107 String dateString = "" ;
108 x = localTime.get(Calendar.YEAR);
109 y = localTime.get(Calendar.MONTH) + 1 ;
110 if (y >= 1 && y <= 3 ) {
111 dateString = x + " - " + " 01 " + " - " + " 01 " ;
112 }
113 if (y >= 4 && y <= 6 ) {
114 dateString = x + " - " + " 04 " + " - " + " 01 " ;
115 }
116 if (y >= 7 && y <= 9 ) {
117 dateString = x + " - " + " 07 " + " - " + " 01 " ;
118 }
119 if (y >= 10 && y <= 12 ) {
120 dateString = x + " - " + " 10 " + " - " + " 01 " ;
121 }
122 return dateString;
123 }
124
125 /**
126 * 功能:得到当前季度季末 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>
127 * @return String
128 * @author pure
129 */
130 public String thisSeasonEnd() {
131 String dateString = "" ;
132 x = localTime.get(Calendar.YEAR);
133 y = localTime.get(Calendar.MONTH) + 1 ;
134 if (y >= 1 && y <= 3 ) {
135 dateString = x + " - " + " 03 " + " - " + " 31 " ;
136 }
137 if (y >= 4 && y <= 6 ) {
138 dateString = x + " - " + " 06 " + " - " + " 30 " ;
139 }
140 if (y >= 7 && y <= 9 ) {
141 dateString = x + " - " + " 09 " + " - " + " 30 " ;
142 }
143 if (y >= 10 && y <= 12 ) {
144 dateString = x + " - " + " 12 " + " - " + " 31 " ;
145 }
146 return dateString;
147 }
148
149 /**
150 * 功能:得到当前年份年初 格式为:xxxx-yy-zz (eg: 2007-01-01)<br>
151 * @return String
152 * @author pure
153 */
154 public String thisYear() {
155 x = localTime.get(Calendar.YEAR);
156 return x + " -01 " + " -01 " ;
157 }
158
159 /**
160 * 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>
161 * @return String
162 * @author pure
163 */
164 public String thisYearEnd() {
165 x = localTime.get(Calendar.YEAR);
166 return x + " -12 " + " -31 " ;
167 }
168
169 /**
170 * 功能:判断输入年份是否为闰年<br>
171 *
172 * @param year
173 * @return 是:true 否:false
174 * @author pure
175 */
176 public boolean leapYear( int year) {
177 boolean leap;
178 if (year % 4 == 0 ) {
179 if (year % 100 == 0 ) {
180 if (year % 400 == 0 ) leap = true ;
181 else leap = false ;
182 }
183 else leap = true ;
184 }
185 else leap = false ;
186 return leap;
187 }
188 }
189