package com.fh.util;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.time.DateUtils;
import java.security.InvalidParameterException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;
public class DateUtil {
private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
private static final SimpleDateFormat sdf = new SimpleDateFormat();
/**
* 获得当前时间并传入格式
*
* @return
*/
public static String getDayString(String pattern) {
try {
sdf.applyPattern(pattern);
return sdf.format(new Date());
} catch (Exception e) {
e.printStackTrace();
System.out.println("获取当前时间失败getDayString");
return null;
}
}
/**
* 获得当前时间long
*
* @return
*/
public static Long getDayLong() {
return new Date().getTime();
}
/**
* String转long类型时间格式,
*
* @return
*/
public static Long getDayStringToLong(String date, String pattern) {
try {
sdf.applyPattern(StringUtils.isNotBlank(pattern) ? pattern : DateUtil.parsePatterns[0]);
return sdf.parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* String转Date类型时间格式,
*
* @return
*/
public static Date getDayStringToDate(String date) {
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
System.out.println("String转Date类型时间失败");
return null;
}
}
/**
* Date转String类型时间格式,
*
* @return
*/
public static String getDayDateToString(Date date, String pattern) {
try {
sdf.applyPattern(pattern);
return sdf.format(date);
} catch (Exception e) {
System.out.println("Date转String类型失败");
return null;
}
}
/**
* long转String类型时间格式,
*
* @return
*/
public static String getDaylongToString(Long date, String pattern) {
sdf.applyPattern(StringUtils.isNotBlank(pattern) ? pattern : DateUtil.parsePatterns[0]);
return sdf.format(new Date(date));
}
/**
* 传入时间和格式,获得格式化时间(只能从长往短转,例如yyyy-MM-dd转yyyy)
*
* @return
*/
public static String getDayStringFormat(String date, String pattern) {
try {
sdf.applyPattern(pattern);
return sdf.format(sdf.parse(date));
} catch (Exception e) {
e.printStackTrace();
System.out.println("格式化时间失败");
return null;
}
}
/**
* @param date
* @return
*/
public static long getMillis(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
/**
* @param s
* @param e
* @return boolean
* @Title: compareDate
* @Description:(日期比较,如果s>=e 返回true 否则返回false)
*/
public static boolean compareDate(String s, String e) {
if (fomatDate(s) == null || fomatDate(e) == null) {
return false;
}
return fomatDate(s).getTime() >= fomatDate(e).getTime();
}
/**
* <li>功能描述:时间相减得到相隔的天数
*
* @param beginDateStr
* @param endDateStr
* @author Administrator
*/
public static long getDaySub(String beginDateStr, String endDateStr) {
long day = 0;
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
java.util.Date beginDate = null;
java.util.Date endDate = null;
try {
beginDate = format.parse(beginDateStr);
endDate = format.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
//System.out.println("相隔的天数="+day);
return day;
}
public static int getMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(2) + 1;
}
public static int getDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(5);
}
public static int getHour(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(12);
}
public static int getSecond(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(13);
}
/**
* 获得 时间是星期几
*
* @return
*/
public static Integer getDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
private static String getYesterday() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
return String.valueOf(getDayDateToString(calendar.getTime(), "yyyy-MM-dd"));
}
private static String getYesterMonth() {
Calendar calendar = Calendar.getInstance();
return String.valueOf(calendar.get(Calendar.MONTH));
}
private static String getYesterYear() {
Calendar calendar = Calendar.getInstance();
return String.valueOf(calendar.get(Calendar.YEAR) - 1);
}
public static Date addDate(Date date, int day) {
Calendar calendar = Calendar.getInstance();
long millis = getMillis(date) + day * 24L * 3600L * 1000L;
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
public static Date minDate(Date date, int day) {
Calendar calendar = Calendar.getInstance();
long millis = getMillis(date) - day * 24L * 3600L * 1000L;
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
public static Date parseDate(String dateStr) {
try {
return DateUtils.parseDate(dateStr, parsePatterns);
} catch (ParseException e) {
throw new InvalidParameterException(dateStr + "不是一个正确格式的日期。");
}
}
public static void main(String[] args) {
System.out.println(getDayString("yyyy-MM-dd"));
System.out.println(getDayStringToDate("2017-03-30"));
System.out.println(getAfterDayWeek("3"));
System.out.println(getDayLong());
System.out.println(getDayStringFormat("2007-01-21", "yyyy"));
System.out.println(compareDate("2017-03-30", "2017-03-20"));
System.out.println("相差天数:" + getDaySub("2017-03-30", "2017-03-20"));
System.out.println("日:" + getDay(new Date()));
System.out.println("时:" + getHour(new Date()));
System.out.println("分:" + getMinute(new Date()));
System.out.println("秒:" + getSecond(new Date()));
System.out.println("星期:" + getDayOfWeek(new Date()));
System.out.println("昨天:" + getYesterday());
System.out.println("上个月:" + getYesterMonth());
System.out.println("去年:" + getYesterYear());
System.out.println("增加后的时间:" + getDayDateToString(addDate(new Date(), 2), "yyyy-MM-dd"));
System.out.println("减少后的时间:" + getDayDateToString(minDate(new Date(), 2), "yyyy-MM-dd"));
}
}
时间工具类
最新推荐文章于 2023-07-15 12:13:22 发布