博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java时间工具类(项目实用)
阅读量:5173 次
发布时间:2019-06-13

本文共 8618 字,大约阅读时间需要 28 分钟。

package com.eabax.plugin.yundada.utils;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.LinkedHashMap;public class TimeUtil {	/**	 * 解析一个日期之间的所有月份	 * 	 * @param beginDateStr	 * @param endDateStr	 * @return	 */	public static ArrayList
getMonthList(String beginDateStr, String endDateStr) { // 指定要解析的时间格式 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM"); // 返回的月份列表 String sRet = ""; // 定义一些变量 Date beginDate = null; Date endDate = null; GregorianCalendar beginGC = null; GregorianCalendar endGC = null; ArrayList
list = new ArrayList
(); try { // 将字符串parse成日期 beginDate = f.parse(beginDateStr); endDate = f.parse(endDateStr); // 设置日历 beginGC = new GregorianCalendar(); beginGC.setTime(beginDate); endGC = new GregorianCalendar(); endGC.setTime(endDate); // 直到两个时间相同 while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) { sRet = beginGC.get(Calendar.YEAR) + "-" + (beginGC.get(Calendar.MONTH) + 1); list.add(sRet); // 以月为单位,增加时间 beginGC.add(Calendar.MONTH, 1); } return list; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 解析一个日期段之间的所有日期 * * @param beginDateStr * 开始日期 * @param endDateStr * 结束日期 * @return */ public static ArrayList
getDayList(String beginDateStr, String endDateStr) { // 指定要解析的时间格式 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); // 定义一些变量 Date beginDate = null; Date endDate = null; Calendar beginGC = null; Calendar endGC = null; ArrayList
list = new ArrayList
(); try { // 将字符串parse成日期 beginDate = f.parse(beginDateStr); endDate = f.parse(endDateStr); // 设置日历 beginGC = Calendar.getInstance(); beginGC.setTime(beginDate); endGC = Calendar.getInstance(); endGC.setTime(endDate); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 直到两个时间相同 while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) { list.add(sdf.format(beginGC.getTime())); // 以日为单位,增加时间 beginGC.add(Calendar.DAY_OF_MONTH, 1); } return list; } catch (Exception e) { e.printStackTrace(); return null; } } public static ArrayList
getYearList() { ArrayList
list = new ArrayList
(); Calendar c = null; c = Calendar.getInstance(); c.setTime(new Date()); int currYear = Calendar.getInstance().get(Calendar.YEAR); int startYear = currYear - 5; int endYear = currYear + 10; for (int i = startYear; i < endYear; i++) { list.add(new Integer(i)); } return list; } public static int getCurrYear() { return Calendar.getInstance().get(Calendar.YEAR); } /** * 得到某一年周的总数 * * @param year * @return */ public static LinkedHashMap
getWeekList(int year) { LinkedHashMap
map = new LinkedHashMap
(); Calendar c = new GregorianCalendar(); c.set(year, Calendar.DECEMBER, 31, 23, 59, 59); int count = getWeekOfYear(c.getTime()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String dayOfWeekStart = ""; String dayOfWeekEnd = ""; for (int i = 1; i <= count; i++) { dayOfWeekStart = sdf.format(getFirstDayOfWeek(year, i)); dayOfWeekEnd = sdf.format(getLastDayOfWeek(year, i)); map.put(new Integer(i), "第" + i + "周(从" + dayOfWeekStart + "至" + dayOfWeekEnd + ")"); } return map; } /** * 得到一年的总周数 * * @param year * @return */ public static int getWeekCountInYear(int year) { Calendar c = new GregorianCalendar(); c.set(year, Calendar.DECEMBER, 31, 23, 59, 59); int count = getWeekOfYear(c.getTime()); return count; } /** * 取得当前日期是多少周 * * @param date * @return */ public static int getWeekOfYear(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setMinimalDaysInFirstWeek(7); c.setTime(date); return c.get(Calendar.WEEK_OF_YEAR); } /** * 得到某年某周的第一天 * * @param year * @param week * @return */ public static Date getFirstDayOfWeek(int year, int week) { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); return getFirstDayOfWeek(cal.getTime()); } /** * 得到某年某周的最后一天 * * @param year * @param week * @return */ public static Date getLastDayOfWeek(int year, int week) { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); return getLastDayOfWeek(cal.getTime()); } /** * 得到某年某月的第一天 * * @param year * @param month * @return */ public static Date getFirestDayOfMonth(int year, int month) { month = month - 1; Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); @SuppressWarnings("static-access") int day = c.getActualMinimum(c.DAY_OF_MONTH); c.set(Calendar.DAY_OF_MONTH, day); return c.getTime(); } /** * 提到某年某月的最后一天 * * @param year * @param month * @return */ public static Date getLastDayOfMonth(int year, int month) { month = month - 1; Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month); @SuppressWarnings("static-access") int day = c.getActualMaximum(c.DAY_OF_MONTH); c.set(Calendar.DAY_OF_MONTH, day); return c.getTime(); } /** * 取得当前日期所在周的第一天 * * @param date * @return */ public static Date getFirstDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday return c.getTime(); } /** * 取得当前日期所在周的最后一天 * * @param date * @return */ public static Date getLastDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday return c.getTime(); } /** * 得到某年某季度第一天 * * @param year * @param quarter * @return */ public static Date getFirstDayOfQuarter(int year, int quarter) { int month = 0; if (quarter > 4) { return null; } else { month = (quarter - 1) * 3 + 1; } return getFirstDayOfMonth(year, month); } /** * 得到某年某季度最后一天 * * @param year * @param quarter * @return */ public static Date getLastDayOfQuarter(int year, int quarter) { int month = 0; if (quarter > 4) { return null; } else { month = quarter * 3; } return getLastDayOfMonth(year, month); } /** * 得到某年第一天 * * @param year * @return */ public static Date getFirstDayOfYear(int year) { return getFirstDayOfQuarter(year, 1); } /** * 得到某年最后一天 * * @param year * @return */ public static Date getLastDayOfYear(int year) { return getLastDayOfQuarter(year, 4); } /** * 得到某年某月的第一天 * * @param year * @param month * @return */ public static Date getFirstDayOfMonth(int year, int month) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH)); return c.getTime(); } /** * 获取某年某月某一周的第一天和最后一天(周一为第一天)(flag=true表示取第一天) * * @param year * @param month * @param week * @param flag * @return */ public static String getDayByWeek(int year, int month, int week, boolean flag) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.WEEK_OF_MONTH, week); int dw = cal.get(Calendar.DAY_OF_WEEK); if (!flag) cal.setTimeInMillis(cal.getTimeInMillis() + (8 - dw) * 24 * 60 * 60 * 1000); else cal.setTimeInMillis(cal.getTimeInMillis() - (dw - 2) * 24 * 60 * 60 * 1000); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String showTime = formatter.format(cal.getTime()); return showTime.toString(); } public static String getStringToDate(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(date); } /** * 转化字符串时间格式 * * @param strTime * @return * @throws ParseException */ public static String formatTimeString(String strTime) throws ParseException { SimpleDateFormat getsdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.sss"); SimpleDateFormat resdf = new SimpleDateFormat("yyyy年MM月dd日"); return resdf.format(getsdf.parse(strTime)); } /** * 获取某一个月的周数 * @param date * @throws ParseException * @throws Exception */ public static int getMonthWeek(String date) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Date s = sdf.parse(date); Calendar ca = Calendar.getInstance(); ca.setTime(s); ca.setFirstDayOfWeek(Calendar.MONDAY); int weeks = ca.getActualMaximum(Calendar.WEEK_OF_MONTH); return weeks; };}

  

转载于:https://www.cnblogs.com/wck1988/p/4211431.html

你可能感兴趣的文章
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
使用Reporting Services时遇到的小问题
查看>>
约瑟夫问题
查看>>
Arduino 报错总结
查看>>
树莓派Android Things物联网开发:树莓派GPIO引脚图
查看>>
矩阵快速幂---BestCoder Round#8 1002
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
awk变量
查看>>
mysql_对于DQL 的简单举例
查看>>
35. Search Insert Position(C++)
查看>>
[毕业生的商业软件开发之路]C#异常处理
查看>>
有关快速幂取模
查看>>
NOI2018垫底记
查看>>
注意java的对象引用
查看>>
C++ 面向对象 类成员函数this指针
查看>>
NSPredicate的使用,超级强大
查看>>
自动分割mp3等音频视频文件的脚本
查看>>
判断字符串是否为空的注意事项
查看>>
布兰诗歌
查看>>
(转)Tomcat 8 安装和配置、优化
查看>>