赞
踩
在我们开发中经常要对时间进行处理,把这些处理方法做成一个工具类是十分方面的,下面是我整理的一些时间处理的方法。
DateUlits方法目录:
1、打印当前日期
2、输入年-月-日 转化为date类型
3、获得一个 Date 对象实例
4、设置时间
5、获取当前时间的前一天时间
6、获取当前时间的后一天时间
7、判断时间是否在某个时间区间内
8、判断是否是今天
9、系统当前时间转化为秒
10、字符串转换为对应日期
11、Date类型转为指定格式的String类型
12、获取前一天的时间的 年-月-日
13、获取当前时间的 年-月-日
14、将当前时间秒的形式(1538129545)转换为年月日-时分秒(2018-09-28 18:12:25)
15、将当前时间秒的形式(1538129545)转换为年月日(2018-09-28)
16、当前时间秒的形式转化为 HH:mm:ss
17、判断目标日期距离当前日期多长时间
18、计算当前实际爱你和系统时间相差几秒
19、获取当天中午12点的时间(毫秒)
20、获取下一天中午12点的时间(毫秒)
21、调用这个方法 你传入一个日期 就能获得 这个日期所在一周内的所有天数的一个List
22、根据日期获得所在周的日期
23、把毫秒转化成日期
24、获取当天的开始时间
25、获取当天的结束时间
26、获取昨天的开始时间
27、获取昨天的结束时间
28、获取明天的开始时间
29、 获取明天的结束时间
30、获取本周的开始时间
31、获取本周的结束时间
32、获取本月的开始时间
33、获取本月的结束时间
34、获取本年的开始时间
35、 获取本年的结束时间
36、获取某个日期的开始时间
37、获取某个日期的结束时间
38、获取今年是哪一年
39、获取本月是哪一月
40、两个日期相减得到的天数
41、 两个日期相减得到的毫秒数
42、获取两个日期中的最大日期
43、获取两个日期中的最小日期
44、返回某月该季度的第一个月
45、返回某个日期下几天的日期
46、返回某个日期前几天的日期
47、获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
48、获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
代码:
- import java.sql.Timestamp;
- 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.List;
-
- public class DateUtil {
-
- //打印当前日期
- public static String printDate() {
- Date currentTime = new Date();
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String dateString = formatter.format(currentTime);
- return dateString;
- }
-
- /**输入年-月-日 转化为date类型**/
- public static Date calendarToData(int year, int month, int day) {
- Calendar calendar = Calendar.getInstance();//日历类的实例化
- calendar.set(year, month - 1, day);//设置日历时间,月份必须减一
- Date date = calendar.getTime(); // 从一个 Calendar 对象中获取 Date 对象
- return date;
- }
-
- /**获得一个 Date 对象实例**/
- public static Calendar dataToCalendar(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar;
- }
-
- /**
- * 设置时间
- * @param year
- * @param month
- * @param date
- * @return
- */
- public static Calendar setCalendar(int year,int month,int date){
- Calendar cl = Calendar.getInstance();
- cl.set(year, month-1, date);
- return cl;
- }
-
- /**
- * 获取当前时间的前一天时间
- * @param cl
- * @return
- */
- public static Calendar getBeforeDay(Calendar cl){
- //使用roll方法进行向前回滚
- //cl.roll(Calendar.DATE, -1);
- //使用set方法直接进行设置
- int day = cl.get(Calendar.DATE);
- cl.set(Calendar.DATE, day-1);
- return cl;
- }
-
- /**
- * 获取当前时间的后一天时间
- * @param cl
- * @return
- */
- public static Calendar getAfterDay(Calendar cl){
- //使用roll方法进行回滚到后一天的时间
- //cl.roll(Calendar.DATE, 1);
- //使用set方法直接设置时间值
- int day = cl.get(Calendar.DATE);
- cl.set(Calendar.DATE, day+1);
- return cl;
- }
-
- /**
- * 打印时间
- * @param cl
- */
- public static void printCalendar(Calendar cl){
- int year = cl.get(Calendar.YEAR);
- int month = cl.get(Calendar.MONTH)+1;
- int day = cl.get(Calendar.DATE);
- System.out.println(year+"-"+month+"-"+day);
- }
-
- /**判断时间是否在某个时间区间内**/
- public static boolean isInDates(String strDate, String strDateBegin,
- String strDateEnd) {
- SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date myDate = null;
- Date dateBegin = null;
- Date dateEnd = null;
- try {
- myDate = sd.parse(strDate);
- dateBegin = sd.parse(strDateBegin);
- dateEnd = sd.parse(strDateEnd);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- strDate = String.valueOf(myDate);
- strDateBegin = String.valueOf(dateBegin);
- strDateEnd = String.valueOf(dateEnd);
-
- int strDateH = Integer.parseInt(strDate.substring(11, 13));
- int strDateM = Integer.parseInt(strDate.substring(14, 16));
- int strDateS = Integer.parseInt(strDate.substring(17, 19));
-
- int strDateBeginH = Integer.parseInt(strDateBegin.substring(11, 13));
- int strDateBeginM = Integer.parseInt(strDateBegin.substring(14, 16));
- int strDateBeginS = Integer.parseInt(strDateBegin.substring(17, 19));
-
- int strDateEndH = Integer.parseInt(strDateEnd.substring(11, 13));
- int strDateEndM = Integer.parseInt(strDateEnd.substring(14, 16));
- int strDateEndS = Integer.parseInt(strDateEnd.substring(17, 19));
-
- if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {
- if (strDateH > strDateBeginH && strDateH < strDateEndH) {
- return true;
- } else if (strDateH == strDateBeginH && strDateM > strDateBeginM
- && strDateH < strDateEndH) {
- return true;
- } else if (strDateH == strDateBeginH && strDateM == strDateBeginM
- && strDateS > strDateBeginS && strDateH < strDateEndH) {
- return true;
- } else if (strDateH == strDateBeginH && strDateM == strDateBeginM
- && strDateS == strDateBeginS && strDateH < strDateEndH) {
- return true;
- } else if (strDateH > strDateBeginH && strDateH == strDateEndH
- && strDateM < strDateEndM) {
- return true;
- } else if (strDateH > strDateBeginH && strDateH == strDateEndH
- && strDateM == strDateEndM && strDateS < strDateEndS) {
- return true;
- } else if (strDateH > strDateBeginH && strDateH == strDateEndH
- && strDateM == strDateEndM && strDateS == strDateEndS) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- /**判断是否是今天**/
- public static boolean isToday(Date date) {
- Calendar c1 = Calendar.getInstance();
- c1.setTime(date);
- int year1 = c1.get(Calendar.YEAR);
- int month1 = c1.get(Calendar.MONTH) + 1;
- int day1 = c1.get(Calendar.DAY_OF_MONTH);
- Calendar c2 = Calendar.getInstance();
- c2.setTime(new Date());
- int year2 = c2.get(Calendar.YEAR);
- int month2 = c2.get(Calendar.MONTH) + 1;
- int day2 = c2.get(Calendar.DAY_OF_MONTH);
- if (year1 == year2 && month1 == month2 && day1 == day2) {
- return true;
- }
- return false;
- }
-
- /**
- * 系统当前时间转化为秒
- *
- * @return
- */
- public static long getUnixStamp() {
- return System.currentTimeMillis() / 1000;
- }
-
- /**
- *
- * 字符串转换为对应日期
- *
- * @param source
- * @param pattern "yyyy-MM-dd"
- * @return
- */
- public static Date stringToDate(String source, String pattern) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
- Date date = null;
- try {
- date = simpleDateFormat.parse(source);
- } catch (Exception e) {
- }
- return date;
- }
-
- /**
- * Date类型转为指定格式的String类型
- *
- * @param source
- * @param pattern
- * @return
- */
- public static String DateToString(Date source, String pattern) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
- return simpleDateFormat.format(source);
- }
-
- /**
- * 获取前一天的时间的 年-月-日
- *
- * @return
- */
- public static String getYestoryDate() {
- Calendar calendar = Calendar.getInstance();
- calendar.add(Calendar.DATE, -1);
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String yestoday = sdf.format(calendar.getTime());
- return yestoday;
- }
-
- /**
- * 获取当前时间的 年-月-日
- *
- * @return
- */
- public static String getTodayDate() {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String date = sdf.format(new Date());
- return date;
- }
-
- /**
- * 将当前时间秒的形式(1538129545)转换为年月日-时分秒(2018-09-28 18:12:25)
- *
- * @param timeStamp
- * @return
- */
- public static String timeStampToStr(long timeStamp) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String date = sdf.format(timeStamp * 1000);
- return date;
- }
-
- /**
- * 将当前时间秒的形式(1538129545)转换为年月日(2018-09-28)
- *
- * @param timeStamp
- * ʱ���
- * @return
- */
- public static String formatDate(long timeStamp) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- String date = sdf.format(timeStamp * 1000);
- return date;
- }
-
- /**
- * 当前时间秒的形式转化为 HH:mm:ss
- *
- * @param timeStamp
- * ʱ���
- * @return
- */
- public static String getTime(long timeStamp) {
- String time = null;
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- String date = sdf.format(timeStamp * 1000);
- String[] split = date.split("\\s");
- if (split.length > 1) {
- time = split[1];
- }
- return time;
- }
-
- /**
- * 判断目标日期距离当前日期多长时间
- *
- * @param timeStamp
- * @return
- */
- public static String convertTimeToFormat(long timeStamp) {
- long curTime = System.currentTimeMillis() / (long) 1000;
- long time = curTime - timeStamp;
-
- if (time < 60 && time >= 0) {
- return "刚刚";
- } else if (time >= 60 && time < 3600) {
- return time / 60 + "分钟";
- } else if (time >= 3600 && time < 3600 * 24) {
- return time / 3600 + "小时";
- } else if (time >= 3600 * 24 && time < 3600 * 24 * 30) {
- return time / 3600 / 24 + "天";
- } else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
- return time / 3600 / 24 / 30 + "月";
- } else if (time >= 3600 * 24 * 30 * 12) {
- return time / 3600 / 24 / 30 / 12 + "年";
- } else {
- return "刚刚";
- }
- }
-
- /**
- *计算当前实际爱你和系统时间相差几秒
- *
- * @param timeStamp
- * @return
- */
- public static String timeStampToFormat(long timeStamp) {
- long curTime = System.currentTimeMillis() / (long) 1000;
- long time = curTime - timeStamp;
- return time / 60 + "";
- }
-
- /** 获取当天中午12点的时间(毫秒) **/
- public static Long getStartTime() {
- Calendar todayStart = Calendar.getInstance();
- todayStart.set(Calendar.HOUR, 0);
- todayStart.set(Calendar.MINUTE, 0);
- todayStart.set(Calendar.SECOND, 0);
- todayStart.set(Calendar.MILLISECOND, 0);
- return todayStart.getTime().getTime();
- }
-
- /**获取下一天中午12点的时间(毫秒)**/
- public static Long getEndTime() {
- Calendar todayEnd = Calendar.getInstance();
- todayEnd.set(Calendar.HOUR, 23);
- todayEnd.set(Calendar.MINUTE, 59);
- todayEnd.set(Calendar.SECOND, 59);
- todayEnd.set(Calendar.MILLISECOND, 999);
- return todayEnd.getTime().getTime();
- }
-
- // 调用这个方法 你传入一个日期 就能获得 这个日期所在一周内的所有天数的一个List
- @SuppressWarnings({ "deprecation", "unchecked" })
- public static List<Date> dateToWeek(Date mdate) {
- // System.out.println("mdate :" + mdate);
- int b = mdate.getDay();
- // System.out.println("b: " + b);
- Date fdate;
- List<Date> list = new ArrayList();
- Long fTime = mdate.getTime() - b * 24 * 3600000;
- // System.out.println("fTime: " + fTime);
- for (int a = 0; a < 8; a++) {
- fdate = new Date();
- fdate.setTime(fTime + (a * 24 * 3600000));
- list.add(a, fdate);
- }
-
- return list;
- }
-
- /**
- * 根据日期获得所在周的日期
- *
- * @param mdate
- * @return
- */
- @SuppressWarnings("deprecation")
- public static List<Date> dateOfWeek(Date mdate) {
- int b = mdate.getDay();
- Date fdate;
- List<Date> list = new ArrayList<Date>();
- Long fTime = mdate.getTime() - b * 24 * 3600000;
- for (int a = 1; a <= 7; a++) {
- fdate = new Date();
- fdate.setTime(fTime + (a * 24 * 3600000));
- list.add(a - 1, fdate);
- }
- return list;
- }
-
- /**
- * 把毫秒转化成日期
- *
- * @param dateFormat
- * (日期格式,例如:MM/ dd/yyyy HH:mm:ss)
- * @param millSec
- * (毫秒数)
- * @return
- */
- private static String transferLongToDate(String dateFormat, Long millSec) {
- SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
- Date date = new Date(millSec);
- return sdf.format(date);
- }
-
- // 获取当天的开始时间
- public static Date getDayBegin() {
- Calendar cal = new GregorianCalendar();
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.SECOND, 0);
- cal.set(Calendar.MILLISECOND, 0);
- return cal.getTime();
- }
-
- // 获取当天的结束时间
- public static Date getDayEnd() {
- Calendar cal = new GregorianCalendar();
- cal.set(Calendar.HOUR_OF_DAY, 23);
- cal.set(Calendar.MINUTE, 59);
- cal.set(Calendar.SECOND, 59);
- return cal.getTime();
- }
-
- // 获取昨天的开始时间
- public static Date getBeginDayOfYesterday() {
- Calendar cal = new GregorianCalendar();
- cal.setTime(getDayBegin());
- cal.add(Calendar.DAY_OF_MONTH, -1);
- return cal.getTime();
- }
-
- // 获取昨天的结束时间
- public static Date getEndDayOfYesterDay() {
- Calendar cal = new GregorianCalendar();
- cal.setTime(getDayEnd());
- cal.add(Calendar.DAY_OF_MONTH, -1);
- return cal.getTime();
- }
-
- // 获取明天的开始时间
- public static Date getBeginDayOfTomorrow() {
- Calendar cal = new GregorianCalendar();
- cal.setTime(getDayBegin());
- cal.add(Calendar.DAY_OF_MONTH, 1);
-
- return cal.getTime();
- }
-
- // 获取明天的结束时间
- public static Date getEndDayOfTomorrow() {
- Calendar cal = new GregorianCalendar();
- cal.setTime(getDayEnd());
- cal.add(Calendar.DAY_OF_MONTH, 1);
- return cal.getTime();
- }
-
- // 获取本周的开始时间
- public static Date getBeginDayOfWeek() {
- Date date = new Date();
- if (date == null) {
- return null;
- }
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
- if (dayofweek == 1) {
- dayofweek += 7;
- }
- cal.add(Calendar.DATE, 2 - dayofweek);
- return getDayStartTime(cal.getTime());
- }
-
- // 获取本周的结束时间
- public static Date getEndDayOfWeek() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getBeginDayOfWeek());
- cal.add(Calendar.DAY_OF_WEEK, 6);
- Date weekEndSta = cal.getTime();
- return getDayEndTime(weekEndSta);
- }
-
- // 获取本月的开始时间
- public static Date getBeginDayOfMonth() {
- Calendar calendar = Calendar.getInstance();
- calendar.set(getNowYear(), getNowMonth() - 1, 1);
- return getDayStartTime(calendar.getTime());
- }
-
- // 获取本月的结束时间
- public static Date getEndDayOfMonth() {
- Calendar calendar = Calendar.getInstance();
- calendar.set(getNowYear(), getNowMonth() - 1, 1);
- int day = calendar.getActualMaximum(5);
- calendar.set(getNowYear(), getNowMonth() - 1, day);
- return getDayEndTime(calendar.getTime());
- }
-
- // 获取本年的开始时间
- public static Date getBeginDayOfYear() {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, getNowYear());
- // cal.set
- cal.set(Calendar.MONTH, Calendar.JANUARY);
- cal.set(Calendar.DATE, 1);
-
- return getDayStartTime(cal.getTime());
- }
-
- // 获取本年的结束时间
- public static Date getEndDayOfYear() {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.YEAR, getNowYear());
- cal.set(Calendar.MONTH, Calendar.DECEMBER);
- cal.set(Calendar.DATE, 31);
- return getDayEndTime(cal.getTime());
- }
-
- // 获取某个日期的开始时间
- public static Timestamp getDayStartTime(Date d) {
- Calendar calendar = Calendar.getInstance();
- if (null != d)
- calendar.setTime(d);
- calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
- calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- calendar.set(Calendar.MILLISECOND, 0);
- return new Timestamp(calendar.getTimeInMillis());
- }
-
- // 获取某个日期的结束时间
- public static Timestamp getDayEndTime(Date d) {
- Calendar calendar = Calendar.getInstance();
- if (null != d)
- calendar.setTime(d);
- calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
- calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
- calendar.set(Calendar.MILLISECOND, 999);
- return new Timestamp(calendar.getTimeInMillis());
- }
-
- // 获取今年是哪一年
- public static Integer getNowYear() {
- Date date = new Date();
- GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
- gc.setTime(date);
- return Integer.valueOf(gc.get(1));
- }
-
- // 获取本月是哪一月
- public static int getNowMonth() {
- Date date = new Date();
- GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
- gc.setTime(date);
- return gc.get(2) + 1;
- }
-
- // 两个日期相减得到的天数
- public static int getDiffDays(Date beginDate, Date endDate) {
-
- if (beginDate == null || endDate == null) {
- throw new IllegalArgumentException("getDiffDays param is null!");
- }
-
- long diff = (endDate.getTime() - beginDate.getTime())
- / (1000 * 60 * 60 * 24);
-
- int days = new Long(diff).intValue();
-
- return days;
- }
-
- // 两个日期相减得到的毫秒数
- public static long dateDiff(Date beginDate, Date endDate) {
- long date1ms = beginDate.getTime();
- long date2ms = endDate.getTime();
- return date2ms - date1ms;
- }
-
- // 获取两个日期中的最大日期
- public static Date max(Date beginDate, Date endDate) {
- if (beginDate == null) {
- return endDate;
- }
- if (endDate == null) {
- return beginDate;
- }
- if (beginDate.after(endDate)) {
- return beginDate;
- }
- return endDate;
- }
-
- // 获取两个日期中的最小日期
- public static Date min(Date beginDate, Date endDate) {
- if (beginDate == null) {
- return endDate;
- }
- if (endDate == null) {
- return beginDate;
- }
- if (beginDate.after(endDate)) {
- return endDate;
- }
- return beginDate;
- }
-
- // 返回某月该季度的第一个月
- public static Date getFirstSeasonDate(Date date) {
- final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 };
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int sean = SEASON[cal.get(Calendar.MONTH)];
- cal.set(Calendar.MONTH, sean * 3 - 3);
- return cal.getTime();
- }
-
- // 返回某个日期下几天的日期
- public static Date getNextDay(Date date, int i) {
- Calendar cal = new GregorianCalendar();
- cal.setTime(date);
- cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
- return cal.getTime();
- }
-
- // 返回某个日期前几天的日期
- public static Date getFrontDay(Date date, int i) {
- Calendar cal = new GregorianCalendar();
- cal.setTime(date);
- cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
- return cal.getTime();
- }
-
- // 获取某年某月到某年某月按天的切片日期集合(间隔天数的集合)
- public static List getTimeList(int beginYear, int beginMonth, int endYear,
- int endMonth, int k) {
- List list = new ArrayList();
- if (beginYear == endYear) {
- for (int j = beginMonth; j <= endMonth; j++) {
- list.add(getTimeList(beginYear, j, k));
-
- }
- } else {
- {
- for (int j = beginMonth; j < 12; j++) {
- list.add(getTimeList(beginYear, j, k));
- }
-
- for (int i = beginYear + 1; i < endYear; i++) {
- for (int j = 0; j < 12; j++) {
- list.add(getTimeList(i, j, k));
- }
- }
- for (int j = 0; j <= endMonth; j++) {
- list.add(getTimeList(endYear, j, k));
- }
- }
- }
- return list;
- }
-
- // 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合)
- public static List getTimeList(int beginYear, int beginMonth, int k) {
- List list = new ArrayList();
- Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
- int max = begincal.getActualMaximum(Calendar.DATE);
- for (int i = 1; i < max; i = i + k) {
- list.add(begincal.getTime());
- begincal.add(Calendar.DATE, k);
- }
- begincal = new GregorianCalendar(beginYear, beginMonth, max);
- list.add(begincal.getTime());
- return list;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。