已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java得到当月天数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;elsereturn 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.ge
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年医院药房考试试题及答案
- 水刀切割防护试题及答案
- 塔吊防台风专项方案
- 旅游经济初级考试试题及答案
- 2026年G1工业锅炉司炉模拟考试题含答案
- 注册土木工程师(岩土)《专业基础考试》真题及答案
- 2026年注册安全工程师考试《安全生产管理》专项训练试卷(附答案)
- 2026年护士执业资格考试《内科护理学》全真模拟试题(附答案)
- 2025年高校学术交流管理岗笔试题(附答案)
- 食堂节能和环保方案
- 2026青海数字经济发展集团有限公司社会招聘9人笔试备考题库及答案详解
- 2026年国家公务员考试面试题及答案
- 浙江省金华市2026年中考一模 科学卷
- 河南开放大学2026年《版式设计》形考作业1-3答案终考作业答案
- 2026年中考历史考前冲刺:中国+世界(古代史|近代史|现代史) 小论文范文汇编
- 先天性无阴道患者的个案护理
- TSG08-2026《特种设备使用管理规则》解析
- 2026届广东广州市普通高中毕业班综合测试(二)化学(含答案)
- 2024-2025学年福建省福州市台江区四年级(下)期末数学试卷 含解析
- 2025年恩施州鹤峰县选调真题
- 2026贵州贵阳市云岩区选聘社区工作者36人笔试参考题库及答案解析
评论
0/150
提交评论