JAVA常用日历日期时间星期工具类(封装方法非常多)

上传人:ji****72 文档编号:37513220 上传时间:2018-04-17 格式:DOC 页数:20 大小:361.05KB
返回 下载 相关 举报
JAVA常用日历日期时间星期工具类(封装方法非常多)_第1页
第1页 / 共20页
JAVA常用日历日期时间星期工具类(封装方法非常多)_第2页
第2页 / 共20页
JAVA常用日历日期时间星期工具类(封装方法非常多)_第3页
第3页 / 共20页
JAVA常用日历日期时间星期工具类(封装方法非常多)_第4页
第4页 / 共20页
JAVA常用日历日期时间星期工具类(封装方法非常多)_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《JAVA常用日历日期时间星期工具类(封装方法非常多)》由会员分享,可在线阅读,更多相关《JAVA常用日历日期时间星期工具类(封装方法非常多)(20页珍藏版)》请在金锄头文库上搜索。

1、JAVA 常用日历常用日历|日期日期|时间时间|星期工具类(封星期工具类(封装方法非常多)装方法非常多)相信大部分程序语言都需要处理日期、日历和时间相关的数据。本工具类是基于 JAVA 语言, 封装了常用的方法,这些方法来源于各种实际项目,大部分都是会直接用到的。此工具类不依赖第三方 JAR 包,可以直接放入您的项目中使用。方法全是自己手写的,若有不正确之处还请指出。此工具此工具类类会不断更新完善会不断更新完善,有需要的就收藏一下吧。import java.text.ParseException;import java.text.SimpleDateFormat;import java.uti

2、l.Arrays;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;/* 日历日期工具类* decriptiondecription 提供常用的获取日期、星期、周数以及转换操作* authorauthor Zebe* datedate 2014/11/27* versionversion 1.0.2*/public class CalendarUtil private static int WEEKS = 0;private static SimpleDateFormat SDF

3、 = new SimpleDateFormat(“yyyy-MM-dd“);private static SimpleDateFormat SDFT = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss“);/* 获取当前年份* returnreturn String 例如:2014*/public static String getYear() Calendar cd = Calendar.getInstance(); return String.valueOf(cd.get(Calendar.YEAR);/* 获取某个日期中的年份* returnretur

4、n String 例如:2014-05-16 返回 2014*/public static String getYearOfDate(String date) Calendar cd = Calendar.getInstance();try cd.setTime(SDF.parse(date); catch (Exception e) e.printStackTrace();return String.valueOf(cd.get(Calendar.YEAR);/* 获取当前月份* returnreturn String 例如:4*/public static String getMonth(

5、) Calendar cd = Calendar.getInstance(); return String.valueOf(cd.get(Calendar.MONTH) + 1);/* 获取某个日期中的月份* returnreturn String 例如:2014-05-16 返回 5*/public static String getMonthOfDate(String date) Calendar cd = Calendar.getInstance();try cd.setTime(SDF.parse(date); catch (Exception e) e.printStackTrace

6、();return String.valueOf(cd.get(Calendar.MONTH) + 1);/* 获取日期中的英文月份缩写* returnreturn String*/public static String getMonthShortEnNameOfDate(String date) Calendar cd = Calendar.getInstance();String result = “;try cd.setTime(SDF.parse(date);int month = cd.get(Calendar.MONTH) + 1;switch (month) case 1:re

7、sult = “JAN“;break;case 2:result = “FEB“;break;case 3:result = “MAR“;break;case 4:result = “APR“;break;case 5:result = “MAY“;break;case 6:result = “JUN“;break;case 7:result = “JUL“;break;case 8:result = “AUG“;break;case 9:result = “SEP“;break;case 10:result = “OCT“;break;case 11:result = “NOV“;break

8、;case 12:result = “DEC“;break; catch (Exception e) e.printStackTrace();return result;/* 获取当前日期* returnreturn String 例如:23*/public static String getDay() Calendar cd = Calendar.getInstance(); return String.valueOf(cd.get(Calendar.DAY_OF_MONTH);/* 获取某个日期中的日期* returnreturn String 例如:2014-05-16 返回 16*/p

9、ublic static String getDayOfDate(String date) Calendar cd = Calendar.getInstance();try cd.setTime(SDF.parse(date); catch (Exception e) e.printStackTrace();return String.valueOf(cd.get(Calendar.DAY_OF_MONTH);/* 获取昨天日期* returnreturn yyyy-MM-dd*/public static String getYesterday() Calendar cal = Calend

10、ar.getInstance();try cal.setTime(SDF.parse(getToday(); cal.add(Calendar.DAY_OF_YEAR, -1); catch (Exception e) e.printStackTrace();return SDF.format(cal.getTime();/* 获取今天日期* returnreturn yyyy-MM-dd*/public static String getToday() return SDF.format(new Date();/* 获取明天日期* returnreturn yyyy-MM-dd*/publi

11、c static String getTommorow() Calendar cal = Calendar.getInstance();try cal.setTime(SDF.parse(getToday(); cal.add(Calendar.DAY_OF_YEAR, +1); catch (Exception e) e.printStackTrace();return SDF.format(cal.getTime();/* 获取当前日期和时间* returnreturn yyyy-MM-dd HH:mm:ss*/public static String getDateTime() retu

12、rn SDFT.format(new Date();/* 获得当前日期与本周一相差的天数* returnreturn int*/private static int getMondayPlus() Calendar cd = Calendar.getInstance(); / 获得今天是一周的第几天,星期日是第一天,星期二是第二天. int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK); if (dayOfWeek = 1) return -6; else return 2 - dayOfWeek; /* 获得上周星期一的日期* returnreturn y

13、yyy-MM-dd*/public static String getPreMonday() WEEKS-; int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * WEEKS); Date monday = currentDate.getTime(); SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM

14、-dd“);return sdf.format(monday); /* 获得本周星期一的日期* returnreturn yyyy-MM-dd*/public static String getMonday() WEEKS = 0;int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus); Date monday = currentDate.getTime(); Sim

15、pleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd“);return sdf.format(monday); /* 获得下周星期一的日期* returnreturn yyyy-MM-dd*/public static String getNextMonday() WEEKS+; int mondayPlus = getMondayPlus(); GregorianCalendar currentDate = new GregorianCalendar(); currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * WEEKS); Date monday = currentDate.getTime(); SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd“);return sdf.format(monday);/* 获取某一年第几周的星期一* returnreturn yyyy-MM-dd*/public static String getMondayOfWeek(int year, int week) Calendar cal = Calendar.getInstance();cal.setFirstDay

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号