1、获取当前时间,和某个时间进行比较。此时主要拿long型的时间值。
方法如下:
要使用 java.util.Date 。获取当前时间的代码如下
Date date = new Date();
date.getTime() ;
还有一种方式,使用 System.currentTimeMillis() ;
都是得到一个当前的时间的long型的时间的毫秒值,这个值实际上是当前时间值与1970年一月一号零时零分零秒相差的毫秒数
一、获取当前时间, 格式为: yyyy-mm-dd hh-mm-ss
DateFormat.getDateTimeInstance(2, 2, Locale.CHINESE).format(new java.util.Date());
二、获取当前时间, 格式为: yyyy年mm月dd日 上午/下午hh时mm分ss秒
1 DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.CHINESE).format(new java.util.Date());
三、获取当前时间(精确到毫秒), 格式为: yyyy-mm-dd hh:mm:ss.nnn
1 new java.sql.Timestamp(System.currentTimeMillis()).toString();
一. 获取当前系统时间和日期并格式化输出:
1 import java.util.Date;
2 import java.text.SimpleDateFormat;
3 public class NowString {
4 public static void main(String[] args) {
5 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
6 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
7 }
8 }
二. 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:
1、用convert()转化函数:
String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";
System.out.println(rs.getString("convertBookDate"));
2、利用SimpleDateFormat类:
先要输入两个java包:
import java.util.Date;
import java.text.SimpleDateFormat;
然后:
定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);
sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";
输出:
System.out.println(df.format(rs.getDate("bookDate")));
3、java中获取当前日期和时间的方法
1)如何取得年月日、小时分秒?
2)如何取得从1970 年到现在的毫秒数?
3)如何取得某个日期是当月的最后一天?
4)如何格式化日期?
1 import java.sql.Date;
2 import java.text.SimpleDateFormat;
3 import java.util.Calendar;
4
5 public class Demo12 {
6
7 /**
8 * @param args
9 */
10 public static void main(String[] args) {
11 // TODO Auto-generated method stub
12 Calendar c = Calendar.getInstance();
13 System.out.println("年:" + c.get(Calendar.YEAR));
14 System.out.println("月:" + (c.get(Calendar.MONTH) + 1));
15 System.out.println("日:" + c.get(Calendar.DAY_OF_MONTH));
16 System.out.println("24时制小时:" + c.get(Calendar.HOUR_OF_DAY));
17 System.out.println("12时制:" + c.get(Calendar.HOUR));
18 System.out.println("分:" + c.get(Calendar.MINUTE));
19 System.out.println("秒:" + c.get(Calendar.SECOND));
20 System.out.println("今天是一年中的第:" + c.get(Calendar.DAY_OF_YEAR) + "天");
21 //-------毫秒数
22
23 long currentSec = System.currentTimeMillis();
24 System.out.println("毫秒数为:" + currentSec);
25
26 //------日期最后一天
27
28 int maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
29 System.out.println("当前日期最后一天:" + maxDay);
30
31 //-------格式化日期
32
33 String format = "yyyy年MM月dd日 HH:mm:ss";
34 SimpleDateFormat SDF = new SimpleDateFormat(format);
35 String timer = SDF.format(new Date(currentSec));
36 System.out.println("格式化日期后:" + timer);
37
38 }
39
40 }
二
1 import java.util.Date;
2 import java.util.Calendar;
3 import java.text.SimpleDateFormat;
4 public class TestDate{
5 public static void main(String[] args){
6 Date now = new Date();
7 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
8
9 String hehe = dateFormat.format( now );
10 System.out.println(hehe);
11 Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
14 int year = c.get(Calendar.YEAR);
15 int month = c.get(Calendar.MONTH);
16 int date = c.get(Calendar.DATE);
17 int hour = c.get(Calendar.HOUR_OF_DAY);
18 int minute = c.get(Calendar.MINUTE);
19 int second = c.get(Calendar.SECOND);
20 System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second);
21 }
22 }
23 有时候要把String类型的时间转换为Date类型,通过以下的方式,就可以将你刚得到的时间字符串转换为Date类型了。
24 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
25 java.util.Date time=null;
26 try {
27 time= sdf.parse(sdf.format(new Date()));
28 } catch (ParseException e) {
29 e.printStackTrace();
30 }
一个不错日期时间类
1 package com.wondersgroup.cc.cti;
2
3 import java.sql.Timestamp;
4 import java.util.Calendar;
5 import java.util.Date;
6
7 public class TestGetSystemTime {
8
9 static Date nowTime = null;
10
11 /**
12 * @param args
13 */
14 public static void main(String[] args) {
15
16 // 以下为 Clendar 测试代码
17 int yyyy, mm, dd;
18 Calendar c = Calendar.getInstance();
19 yyyy = c.get(Calendar.YEAR);
20 mm = c.get(Calendar.MONTH) + 1;
21 dd = c.get(Calendar.DAY_OF_MONTH);
22 System.out.println("year = "+yyyy);
23 System.out.println("month = "+mm);
24 System.out.println("day = "+dd);
25
26 // 1. System.currentTimeMillis() 获取系统时间的代码
27 Timestamp ts = new Timestamp(System.currentTimeMillis());
28 String dateStr1 = ts.toString();
29 System.out.println("System.currentTimeMillis() = "+dateStr1);
30
31 // 2. date 为获得系统时间的另外一种方法
32 Date date = new Date();
33 String dateStr2 = new Timestamp(date.getTime()).toString();
34 System.out.println(dateStr2);
35
36 // 得到JAVA运行环境的一些基本属性
37 System.getProperties().list(System.out);
38 }
39
40 }
一个完整的获取日期实例
格式化日期 String-->Date 或者 Data-->String
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("2009-11-04");//String-->Date
String sdate = sdf.format(date );// Data-->String
1 package com.hefeng.test;
2 import java.text.DateFormat;
3 import java.text.ParsePosition;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.GregorianCalendar;
8 public class TimeTest {
9 //用来全局控制 上一周,本周,下一周的周数变化
10 private int weeks = 0;
11 private int MaxDate;//一月最大天数
12 private int MaxYear;//一年最大天数
13 /**
14 * @param args
15 */
16 public static void main(String[] args) {
17 TimeTest tt = new TimeTest();
18 System.out.println("得到6个月后的日期:"+tt .getAfterMonth(6);
19 System.out.println("获取当天日期:"+tt.getNowTime("yyyy-MM-dd"));
20 System.out.println("获取本周一日期:"+tt.getMondayOFWeek());
21 System.out.println("获取本周日的日期~:"+tt.getCurrentWeekday());
22 System.out.println("获取上周一日期:"+tt.getPreviousWeekday());
23 System.out.println("获取上周日日期:"+tt.getPreviousWeekSunday());
24 System.out.println("获取下周一日期:"+tt.getNextMonday());
25 System.out.println("获取下周日日期:"+tt.getNextSunday());
26 System.out.println("获得相应周的周六:"+tt.getNowTime("yyyy-MM-dd"));
27 System.out.println("获取本月第一天日期:"+tt.getFirstDayOfMonth());
28 System.out.println("获取本月最后一天日期:"+tt.getDefaultDay());
29 System.out.println("获取上月第一天日期:"+tt.getPreviousMonthFirst());
30 System.out.println("获取上月最后一天的日期:"+tt.getPreviousMonthEnd());
31 System.out.println("获取下月第一天日期:"+tt.getNextMonthFirst());
32 System.out.println("获取下月最后一天日期:"+tt.getNextMonthEnd());
33 System.out.println("获取本年的第一天日期:"+tt.getCurrentYearFirst());
34 System.out.println("获取本年最后一天日期:"+tt.getCurrentYearEnd());
35 System.out.println("获取去年的第一天日期:"+tt.getPreviousYearFirst());
36 System.out.println("获取去年的最后一天日期:"+tt.getPreviousYearEnd());
37 System.out.println("获取明年第一天日期:"+tt.getNextYearFirst());
38 System.out.println("获取明年最后一天日期:"+tt.getNextYearEnd());
39 System.out.println("获取本季度第一天到最后一天:"+tt.getThisSeasonTime(11));
40 System.out.println("获取两个日期之间间隔天数2008-12-1~2008-
41 .29:"+TimeTest.getTwoDay("2008-12-1","2008-9-29"));
42 }
43 /**
44 * 得到指定月后(前)的日期 参数传负数即可
45 */
46 public static String getAfterMonth(int month) {
47 Calendar c = Calendar.getInstance();//获得一个日历的实例
48 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
49 Date date = null;
50 try{
51 date = sdf.parse("2009-11-04");//初始日期
52 }catch(Exception e){
53 }
54 c.setTime(date);//设置日历时间
55 c.add(Calendar.MONTH,month);//在日历的月份上增加6个月
56 String strDate = sdf.format(c.getTime()));//的到你想要得6个月后的日期
57 return strDate;
58 }
59 /**
60 * 得到二个日期间的间隔天数
61 */
62 public static String getTwoDay(String sj1, String sj2) {
63 SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
64 long day = 0;
65 try {
66 java.util.Date date = myFormatter.parse(sj1);
67 java.util.Date mydate = myFormatter.parse(sj2);
68 day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
69 } catch (Exception e) {
70 return "";
71 }
72 return day + "";
73 }
74 /**
75 * 根据一个日期,返回是星期几的字符串
76 */
77 public static String getWeek(String sdate) {
78 // 再转换为时间
79 Date date = TimeTest.strToDate(sdate);
80 Calendar c = Calendar.getInstance();
81 c.setTime(date);
82 // int hour=c.get(Calendar.DAY_OF_WEEK);
83 // hour 中存的就是星期几了,其范围 1~7
84 // 1=星期日 7=星期六,其他类推
85 return new SimpleDateFormat("EEEE").format(c.getTime());
86 }
87 /**
88 * 将短时间格式字符串转换为时间 yyyy-MM-dd
89 */
90 public static Date strToDate(String strDate) {
91 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
92 ParsePosition pos = new ParsePosition(0);
93 Date strtodate = formatter.parse(strDate, pos);
94 return strtodate;
95 }
96 /**
97 * 两个时间之间的天数
98 *
99 * @param date1
100 * @param date2
101 * @return
102 */
103 public static long getDays(String date1, String date2) {
104 if (date1 == null || date1.equals(""))
105 return 0;
106 if (date2 == null || date2.equals(""))
107 return 0;
108 // 转换为标准时间
109 SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
110 java.util.Date date = null;
111 java.util.Date mydate = null;
112 try {
113 date = myFormatter.parse(date1);
114 mydate = myFormatter.parse(date2);
115 } catch (Exception e) {
116 }
117 long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
118 return day;
119 }
120 // 计算当月最后一天,返回字符串
121 public String getDefaultDay(){
122 String str = "";
123 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
124 Calendar lastDate = Calendar.getInstance();
125 lastDate.set(Calendar.DATE,1);//设为当前月的1 号
126 lastDate.add(Calendar.MONTH,1);//加一个月,变为下月的1 号
127 lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天
128 str=sdf.format(lastDate.getTime());
129 return str;
130 }
131 // 上月第一天
132 public String getPreviousMonthFirst(){
133 String str = "";
134 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
135 Calendar lastDate = Calendar.getInstance();
136 lastDate.set(Calendar.DATE,1);//设为当前月的1 号
137 lastDate.add(Calendar.MONTH,-1);//减一个月,变为下月的1 号
138 //lastDate.add(Calendar.DATE,-1);//减去一天,变为当月最后一天
139 str=sdf.format(lastDate.getTime());
140 return str;
141 }
142 //获取当月第一天
143 public String getFirstDayOfMonth(){
144 String str = "";
145 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
146 Calendar lastDate = Calendar.getInstance();
147 lastDate.set(Calendar.DATE,1);//设为当前月的1 号
148 str=sdf.format(lastDate.getTime());
149 return str;
150 }
151 // 获得本周星期日的日期
152 public String getCurrentWeekday() {
153 weeks = 0;
154 int mondayPlus = this.getMondayPlus();
155 GregorianCalendar currentDate = new GregorianCalendar();
156 currentDate.add(GregorianCalendar.DATE, mondayPlus+6);
157 Date monday = currentDate.getTime();
158 DateFormat df = DateFormat.getDateInstance();
159 String preMonday = df.format(monday);
160 return preMonday;
161 }
162 //获取当天时间
163 public String getNowTime(String dateformat){
164 Date now = new Date();
165 SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//可
166 以方便
167 地修改日期格式
168 String hehe = dateFormat.format(now);
169 return hehe;
170 }
171 // 获得当前日期与本周日相差的天数
172 private int getMondayPlus() {
173 Calendar cd = Calendar.getInstance();
174 // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
175 int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1; //因为按中国礼拜一
176 作为第一
177 天所以这里减1
178 if (dayOfWeek == 1) {
179 return 0;
180 } else {
181 return 1 - dayOfWeek;
182 }
183 }
184 //获得本周一的日期
185 public String getMondayOFWeek(){
186 weeks = 0;
187 int mondayPlus = this.getMondayPlus();
188 GregorianCalendar currentDate = new GregorianCalendar();
189 currentDate.add(GregorianCalendar.DATE, mondayPlus);
190 Date monday = currentDate.getTime();
191 DateFormat df = DateFormat.getDateInstance();
192 String preMonday = df.format(monday);
193 return preMonday;
194 }
195 //获得相应周的周六的日期
196 public String getSaturday() {
197 int mondayPlus = this.getMondayPlus();
198 GregorianCalendar currentDate = new GregorianCalendar();
199 currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);
200 Date monday = currentDate.getTime();
201 DateFormat df = DateFormat.getDateInstance();
202 String preMonday = df.format(monday);
203 return preMonday;
204 }
205 // 获得上周星期日的日期
206 public String getPreviousWeekSunday() {
207 weeks=0;
208 weeks--;
209 int mondayPlus = this.getMondayPlus();
210 GregorianCalendar currentDate = new GregorianCalendar();
211 currentDate.add(GregorianCalendar.DATE, mondayPlus+weeks);
212 Date monday = currentDate.getTime();
213 DateFormat df = DateFormat.getDateInstance();
214 String preMonday = df.format(monday);
215 return preMonday;
216 }
217 // 获得上周星期一的日期
218 public String getPreviousWeekday() {
219 weeks--;
220 int mondayPlus = this.getMondayPlus();
221 GregorianCalendar currentDate = new GregorianCalendar();
222 currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
223 Date monday = currentDate.getTime();
224 DateFormat df = DateFormat.getDateInstance();
225 String preMonday = df.format(monday);
226 return preMonday;
227 }
228 // 获得下周星期一的日期
229 public String getNextMonday() {
230 weeks++;
231 int mondayPlus = this.getMondayPlus();
232 GregorianCalendar currentDate = new GregorianCalendar();
233 currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
234 Date monday = currentDate.getTime();
235 DateFormat df = DateFormat.getDateInstance();
236 String preMonday = df.format(monday);
237 return preMonday;
238 }
239 // 获得下周星期日的日期
240 public String getNextSunday() {
241 int mondayPlus = this.getMondayPlus();
242 GregorianCalendar currentDate = new GregorianCalendar();
243 currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6);
244 Date monday = currentDate.getTime();
245 DateFormat df = DateFormat.getDateInstance();
246 String preMonday = df.format(monday);
247 return preMonday;
248 }
249 private int getMonthPlus(){
250 Calendar cd = Calendar.getInstance();
251 int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);
252 cd.set(Calendar.DATE, 1);//把日期设置为当月第一天
253 cd.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
254 MaxDate=cd.get(Calendar.DATE);
255 if(monthOfNumber == 1){
256 return -MaxDate;
257 }else{
258 return 1-monthOfNumber;
259 }
260 }
261 //获得上月最后一天的日期
262 public String getPreviousMonthEnd(){
263 String str = "";
264 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
265 Calendar lastDate = Calendar.getInstance();
266 lastDate.add(Calendar.MONTH,-1);//减一个月
267 lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天
268 lastDate.roll(Calendar.DATE, -1);//日期回滚一天,也就是本月最后一天
269 str=sdf.format(lastDate.getTime());
270 return str;
271 }
272 //获得下个月第一天的日期
273 public String getNextMonthFirst(){
274 String str = "";
275 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
276 Calendar lastDate = Calendar.getInstance();
277 lastDate.add(Calendar.MONTH,1);//减一个月
278 lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天
279 str=sdf.format(lastDate.getTime());
280 return str;
281 }
282 //获得下个月最后一天的日期
283 public String getNextMonthEnd(){
284 String str = "";
285 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
286 Calendar lastDate = Calendar.getInstance();
287 lastDate.add(Calendar.MONTH,1);//加一个月
288 lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天
289 lastDate.roll(Calendar.DATE, -1);//日期回滚一天,也就是本月最后一天
290 str=sdf.format(lastDate.getTime());
291 return str;
292 }
293 //获得明年最后一天的日期
294 public String getNextYearEnd(){
295 String str = "";
296 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
297 Calendar lastDate = Calendar.getInstance();
298 lastDate.add(Calendar.YEAR,1);//加一个年
299 lastDate.set(Calendar.DAY_OF_YEAR, 1);
300 lastDate.roll(Calendar.DAY_OF_YEAR, -1);
301 str=sdf.format(lastDate.getTime());
302 return str;
303 }
304 //获得明年第一天的日期
305 public String getNextYearFirst(){
306 String str = "";
307 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
308 Calendar lastDate = Calendar.getInstance();
309 lastDate.add(Calendar.YEAR,1);//加一个年
310 lastDate.set(Calendar.DAY_OF_YEAR, 1);
311 str=sdf.format(lastDate.getTime());
312 return str;
313 }
314 //获得本年有多少天
315 private int getMaxYear(){
316 Calendar cd = Calendar.getInstance();
317 cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天
318 cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。
319 int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
320 return MaxYear;
321 }
322 private int getYearPlus(){
323 Calendar cd = Calendar.getInstance();
324 int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//获得当天是一年中的第几天
325 cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天
326 cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。
327 int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
328 if(yearOfNumber == 1){
329 return -MaxYear;
330 }else{
331 return 1-yearOfNumber;
332 }
333 }
334 //获得本年第一天的日期
335 public String getCurrentYearFirst(){
336 int yearPlus = this.getYearPlus();
337 GregorianCalendar currentDate = new GregorianCalendar();
338 currentDate.add(GregorianCalendar.DATE,yearPlus);
339 Date yearDay = currentDate.getTime();
340 DateFormat df = DateFormat.getDateInstance();
341 String preYearDay = df.format(yearDay);
342 return preYearDay;
343 }
344 //获得本年最后一天的日期 *
345 public String getCurrentYearEnd(){
346 Date date = new Date();
347 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
348 便地修
349 改日期格式
350 String years = dateFormat.format(date);
351 return years+"-12-31";
352 }
353 //获得上年第一天的日期 *
354 public String getPreviousYearFirst(){
355 Date date = new Date();
356 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
357 便地修
358 改日期格式
359 String years = dateFormat.format(date); int years_value = Integer.parseInt
360 (years);
361 years_value--;
362 return years_value+"-1-1";
363 }
364 //获得上年最后一天的日期
365 public String getPreviousYearEnd(){
366 weeks--;
367 int yearPlus = this.getYearPlus();
368 GregorianCalendar currentDate = new GregorianCalendar();
369 currentDate.add(GregorianCalendar.DATE,yearPlus+MaxYear*weeks+(MaxYear-1));
370 Date yearDay = currentDate.getTime();
371 DateFormat df = DateFormat.getDateInstance();
372 String preYearDay = df.format(yearDay);
373 getThisSeasonTime(11);
374 return preYearDay;
375 }
376 //获得本季度
377 public String getThisSeasonTime(int month){
378 int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
379 int season = 1;
380 if(month>=1&&month<=3){
381 season = 1;
382 }
383 if(month>=4&&month<=6){
384 season = 2;
385 }
386 if(month>=7&&month<=9){
387 season = 3;
388 }
389 if(month>=10&&month<=12){
390 season = 4;
391 }
392 int start_month = array[season-1][0];
393 int end_month = array[season-1][2];
394 Date date = new Date();
395 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
396
397 String years = dateFormat.format(date);
398 int years_value = Integer.parseInt(years);
399 int start_days =1;//years+"-"+String.valueOf(start_month)+"-
400 ";//getLastDayOfMonth(years_value,start_month);
401 int end_days = getLastDayOfMonth(years_value,end_month);
402 String seasonDate =
403 years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;
404 return seasonDate;
405 }
406 /**
407 * 获取某年某月的最后一天
408 * @param year 年
409 * @param month 月
410 * @return 最后一天
411 */
412 private int getLastDayOfMonth(int year, int month) {
413 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
414 || month == 10 || month == 12) {
415 return 31;
416 }
417 if (month == 4 || month == 6 || month == 9 || month == 11) {
418 return 30;
419 }
420 if (month == 2) {
421 if (isLeapYear(year)) {
422 return 29;
423 } else {
424 return 28;
425 }
426 }
427 return 0;
428 }
429 /**
430 * 是否闰年
431 * @param year 年
432 * @return
433 */
434 public boolean isLeapYear(int year) {
435 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
436 }