时间工具类(二)

来源:互联网 发布:java 解析sql获取别名 编辑:程序博客网 时间:2024/05/14 15:02

1.日期工具类

package com.text;import java.sql.Timestamp;import java.text.ParseException;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.Objects;import java.util.TimeZone;/** * 【第三方扩展类库org.apache.commons.lang.time】 */import org.apache.commons.lang.time.DateFormatUtils;import org.apache.commons.lang.time.DateUtils;/** *  * @author Administrator 日期工具类 */public class DateTools2 {// 一分钟的毫秒数private final static long MINUTE_MILLIS = 60000;// 一小时的毫秒数private final static long HOUR_MILLIS = 3600000;// 一天的毫秒数public final static long DAY_MILLIS = 86400000;// 默认时区private final static String DEFAULT_TIME_ZONE = "Asia/Chendu";// 格式化日期public static String formatDate(Date date, String format) {return DateFormatUtils.format(date, format,TimeZone.getTimeZone(DEFAULT_TIME_ZONE),Locale.getDefault(Locale.Category.FORMAT));}// 当前日期public static String getCurrentDate(String format) {return formatDate(Calendar.getInstance().getTime(), format);}// 日期转换 格式:yyyy-MM-dd,yyyy-MM-dd HH:mm:ss等public static Date parseDate(String dateStr, String format) {java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format);java.text.ParsePosition pos = new java.text.ParsePosition(0);return formatter.parse(dateStr, pos);}// 获取指定长度时间 此方法获取格式为yyyy-MM-dd的日期public static Date truncateDate(Date dt) {return DateUtils.truncate(dt, Calendar.DAY_OF_MONTH);}// 获取两日期是否同年月public static boolean isInSameMonth(Date oneDate, Date anotherDate) {Calendar calendar = Calendar.getInstance();calendar.setTime(oneDate);int year = calendar.get(Calendar.YEAR);int month = calendar.get(Calendar.MONTH);Calendar anotherCalendar = Calendar.getInstance();anotherCalendar.setTime(anotherDate);int anotherYear = anotherCalendar.get(Calendar.YEAR);int anotherMonth = anotherCalendar.get(Calendar.MONTH);return (year == anotherYear) & (month == anotherMonth);}// 获取X月后的日期public static Date getAfterMonth(Date date, int x) {return DateUtils.truncate(DateUtils.addMonths(date, x),Calendar.DAY_OF_MONTH);}// 获取x月后的日期,含最后一天public static Date getAfterMonthNext(Date date, int x) {Calendar c = Calendar.getInstance();c.setTimeInMillis(date.getTime());int oldDate = c.get(Calendar.DAY_OF_MONTH);c.add(Calendar.MONTH, x);int newDate = c.get(Calendar.DAY_OF_MONTH);// 判断新月天数是否小于旧月天数,若小于则表明出现了28 < 30, 30 < 31之类的,需顺延if (newDate < oldDate) {c.add(Calendar.DATE, 1); // 加1天}return DateUtils.truncate(c.getTime(), Calendar.DAY_OF_MONTH);}// 获取X分钟后的时间public static Date getDateAfterByMinute(Date date, Integer x) {return DateUtils.addMinutes(date, x);}// 获取x天后的日期public static Date getAfterDay(Date date, int x) {return DateUtils.truncate(DateUtils.addDays(date, x),Calendar.DAY_OF_MONTH);}// 获取x小时后的日期public static Date getDateAfterByHour(Date date, Integer x) {return DateUtils.truncate(DateUtils.addHours(date, x), Calendar.HOUR);}// 获取月数的第一天public static String getFirstDay(Date dt, String formatStr) {return formatDate(DateUtils.truncate(dt, Calendar.MONTH), formatStr);}// 获取月数最后一日日期public static int getLastDateDay(Date dt, String formatStr) {Calendar ca = Calendar.getInstance();ca.setTime(dt);ca.set(Calendar.DAY_OF_MONTH, 1);ca.add(Calendar.MONTH, 1);ca.add(Calendar.DATE, -1);return ca.get(Calendar.DATE);}// 获取月数的最后一天public static String getLastDay(Date dt, String formatStr) {Calendar ca = Calendar.getInstance();ca.setTime(dt);// 该月第一天ca.set(Calendar.DAY_OF_MONTH, 1);// 当月加1ca.add(Calendar.MONTH, 1);// 日期减一ca.add(Calendar.DATE, -1);return formatDate(ca.getTime(), formatStr);}// 两日期相差年数public static int yearPhaseDiffer(Date begindate, Date enddate) {try {int year1 = getYear(begindate);int year2 = getYear(enddate);return year2 - year1;} catch (Exception e) {throw new RuntimeException(e);}}// 两日期相差月数public static int monthPhaseDiffer(Date begindate, Date enddate) {try {int year1 = getYear(begindate);int month1 = getMonth(begindate);int day1 = getDay(begindate);int year2 = getYear(enddate);int month2 = getMonth(enddate);int day2 = getDay(enddate);int month = (year2 - year1) * 12 + month2 - month1;if (day2 - day1 > 0)return month + 1;elsereturn month;} catch (Exception e) {throw new RuntimeException(e);}}// 两日期相差天数public static int datePhaseDiffer(Date beginDate, Date endDate) {try {return (int) ((endDate.getTime() - beginDate.getTime()) / DAY_MILLIS);} catch (Exception e) {throw new RuntimeException(e);}}// 两日期相差小时数public static int hourPhaseDiffer(Date beginDate, Date endDate) {try {return (int) ((endDate.getTime() - beginDate.getTime()) / HOUR_MILLIS);} catch (Exception e) {throw new RuntimeException(e);}}// 两日期相差分钟数public static int minutePhaseDiffer(Date beginDate, Date endDate) {try {return (int) ((endDate.getTime() - beginDate.getTime()) / MINUTE_MILLIS);} catch (Exception e) {throw new RuntimeException(e);}}// 两日期相差秒数public static int secondPhaseDiffer(Date beginDate, Date endDate) {try {return (int) ((endDate.getTime() - beginDate.getTime()) / 1000);} catch (Exception e) {throw new RuntimeException(e);}}// 两日期相差时间精度显示 d:h:spublic static String show(Date start, Date end) {long temp = end.getTime() - start.getTime();String leavingTime = temp / DAY_MILLIS + "天" + (temp % DAY_MILLIS)/ HOUR_MILLIS + "小时" + ((temp % DAY_MILLIS) % HOUR_MILLIS)/ MINUTE_MILLIS + "分";return leavingTime;}// 获取年份public static int getYear(Date dt) {Calendar cal = Calendar.getInstance();cal.setTime(dt);return cal.get(Calendar.YEAR);}// 获取月份public static int getMonth(Date dt) {Calendar cal = Calendar.getInstance();cal.setTime(dt);return cal.get(Calendar.MONTH);}// 获取天public static int getDay(Date dt) {Calendar cal = Calendar.getInstance();cal.setTime(dt);return cal.get(Calendar.DATE);}// 日期大小比较public static boolean compare_date(Date date1, Date date2) {boolean flag = false;if (date1.getTime() >= date2.getTime()) {return true;}return flag;}// 返回格式yyyyMMdd日期public static String showDateString(Date date) {if (null == date) {throw new RuntimeException("日期不能为空");}return formatDate(date, "yyyyMMdd");}// 返回某天起始时间public static Date getStartDate(Date date) {return truncateDate(date);}// 返回某天结束时间public static Date getEndDate(Date date) {return parseDate(DateTools2.formatDate(date, "yyyy-MM-dd")+ " 23:59:59", "yyyy-MM-dd HH:mm:ss");}// 返回第二天日期public static Date getNextDate(Date date) {return getAfterDay(getStartDate(date), 1);}// 返回该月第一天起始时间public static Date getMonthStartDate(Date date) {return DateUtils.truncate(date, Calendar.MONTH);}// 字符串转换为日期public static Date parseStandardDate(String dateStr) {return DateUtils.parseDate(dateStr, "yyyy-MM-dd");}// 日期转换为时间戳public static Timestamp getDateToTimeStamp(Date paramDate) {if (paramDate == null) {return null;}return new Timestamp(paramDate.getTime());}// 获取某日期分钟数public static Date truncateMinute(Date date) {return DateUtils.truncate(date, Calendar.MINUTE);}// 获取某月天数public static int getDaysOfMonth(Date date) {Calendar calendar = Calendar.getInstance();calendar.setTime(date);return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);}// 测试public static void main(String[] args) {// System.out.prin//System.out.println(truncateMinute());}}


另:所有的代码都是为了方便我们的工作,从繁琐复杂的循环中,变得轻松简单,有些时候觉得程序员是一个伟大的工作,是帮助的人类生活变得方便但是,如果我们只是把自己的技术知识只是放在自己的小范围内,自己的大脑中,自己的心中,自己的朋友中,其意义何在呢!,希望大家也可以花一点时间写出你的技术,写出你工作的意义,写出一个程序员的精彩。【welcome here :csdn communication center qq群:678470500】


原创粉丝点击