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);
// 当月加1
ca.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;
else
return 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:s
public 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】