java得到当月天数
来源:互联网 发布:河南数据统计采集门户 编辑:程序博客网 时间:2024/04/30 13:58
1.使用calendar类实现
- /**
- * 取得当月天数
- * */
- public static int getCurrentMonthLastDay()
- {
- Calendar a = Calendar.getInstance();
- a.set(Calendar.DATE, 1);//把日期设置为当月第一天
- a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
- int maxDate = a.get(Calendar.DATE);
- return maxDate;
- }
- /**
- * 得到指定月的天数
- * */
- public static int getMonthLastDay(int year, int month)
- {
- Calendar a = Calendar.getInstance();
- a.set(Calendar.YEAR, year);
- a.set(Calendar.MONTH, month - 1);
- a.set(Calendar.DATE, 1);//把日期设置为当月第一天
- a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
- int maxDate = a.get(Calendar.DATE);
- return maxDate;
- }
2.使用自己编写的函数实现
- package test;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * 日期工具类 by hpf
- * */
- public class DateUtils
- {
- //返回当前年月日
- String getNowDate()
- {
- Date date = new Date();
- String nowDate = new SimpleDateFormat("yyyy年MM月dd日").format(date);
- return nowDate;
- }
- //返回当前年份
- int getYear()
- {
- Date date = new Date();
- String year = new SimpleDateFormat("yyyy").format(date);
- return Integer.parseInt(year);
- }
- //返回当前月份
- int getMonth()
- {
- Date date = new Date();
- String month = new SimpleDateFormat("MM").format(date);
- return Integer.parseInt(month);
- }
- //判断闰年
- boolean isLeap(int year)
- {
- if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0))
- return true;
- else
- return false;
- }
- //返回当月天数
- int getDays(int year, int month)
- {
- int days;
- int FebDay = 28;
- if (isLeap(year))
- FebDay = 29;
- switch (month)
- {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- days = 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- days = 30;
- break;
- case 2:
- days = FebDay;
- break;
- default:
- days = 0;
- break;
- }
- return days;
- }
- //返回当月星期天数
- int getSundays(int year, int month)
- {
- int sundays = 0;
- SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
- Calendar setDate = Calendar.getInstance();
- //从第一天开始
- int day;
- for (day = 1; day <= getDays(year, month); day++)
- {
- setDate.set(Calendar.DATE, day);
- String str = sdf.format(setDate.getTime());
- if (str.equals("星期日"))
- {
- sundays++;
- }
- }
- return sundays;
- }
- public static void main(String[] args)
- {
- DateUtils du = new DateUtils();
- System.out.println("今天日期是:" + du.getNowDate());
- System.out.println("本月有" + du.getDays(du.getYear(), du.getMonth()) + "天");
- System.out.println("本月有" + du.getSundays(du.getYear(), du.getMonth()) + "个星期天");
- }
- }
地址:http://blog.csdn.net/hpf911/article/details/5909963
0 0
- java得到当月天数
- java得到当月天数
- java得到当月天数 .
- java得到当月天数
- java得到当月天数
- java获取当月最大天数
- 【叶子函数分享三十九】根据年月得到当月的天数
- java输入年份,月份,输出当月天数
- JS获得当月的天数和得到当月最后一天的日期
- 获取当月天数当月星期天数
- SQL取当月天数
- 查询当月天数
- 计算当月的天数
- 获取当月的天数。
- python 读取当月天数
- 获取当月天数
- js 获取当月天数
- JS获取当月天数
- saving data - Saving Data in SQL Databases
- 我是如何自学Android,资料分享(2015 版)
- iOS学习笔记(5)——设计模式之工厂方法、抽象工厂
- spring加载bean(ClassPathXmlApplicationContext和FileSystemXmlApplicationContext)
- 总结之linux下的操作
- java得到当月天数
- 登录界面多个账户信息存储问题
- UICollectionView详解
- Android 网络学习总结
- AngularJS 无限滚动加载数据控件 ngInfiniteScroll
- Vmware vSphere(Esxi)常见问题汇总
- 2014十大顶级开源ERP系统点评
- Spring中Ioc入门实例
- 快速建立列表对话框