package com.qycar.common.utils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
-
日期处理
/
public class DateUtils {
/*- 时间格式(yyyy-MM-dd)
/
public static final String DATE_PATTERN = “yyyy-MM-dd”;
public static final String MOTH_PATTERN = “yyyy-MM”;
/* - 时间格式(yyyy-MM-dd HH:mm:ss)
*/
public static final String DATE_TIME_PATTERN = “yyyy-MM-dd HH:mm:ss”;
public static final String format(Date date) {
return format(date, DATE_PATTERN);
}public static String format(Date date, String pattern) {
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
return null;
}/**
- 计算距离现在多久,非精确
- @param date
- @return
*/
public static String getTimeBefore(Date date) {
Date now = new Date();
long l = now.getTime() - date.getTime();
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
String r = “”;
if (day > 0) {
r += day + “天”;
} else if (hour > 0) {
r += hour + “小时”;
} else if (min > 0) {
r += min + “分”;
} else if (s > 0) {
r += s + “秒”;
}
r += “前”;
return r;
}
/**
- 计算距离现在多久,精确
- @param date
- @return
*/
public static String getTimeBeforeAccurate(Date date) {
Date now = new Date();
long l = now.getTime() - date.getTime();
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
String r = “”;
if (day > 0) {
r += day + “天”;
}
if (hour > 0) {
r += hour + “小时”;
}
if (min > 0) {
r += min + “分”;
}
if (s > 0) {
r += s + “秒”;
}
r += “前”;
return r;
}
/**
- 返回某个日期的前一天
*/
public static Date getNextDay(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
return date;
}
/**
- 返回当月最后一天的日期
*/
public static Date getLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.getMaximum(Calendar.DATE));
return calendar.getTime();
}
/**
- 获取当前时间到凌晨还剩多少毫秒
*/
public static long getMilliToDawn(){
long milliSecondsLeftToday = 86400000 -
org.apache.commons.lang3.time.DateUtils.getFragmentInMilliseconds(Calendar.getInstance(), Calendar.DATE);
return milliSecondsLeftToday;
}
/**
- 时间戳矫正(Java中标准的时间戳为13位)
*/
public static long rectifyTimeStamp(long stateTime){
//Java中标准的时间戳为13位
String strTimeStamp = stateTime + “”;
int len = strTimeStamp.length();
for (int i = len; i < 13; i++) {
strTimeStamp += “0”;
}
return Long.valueOf(strTimeStamp);
}
/**
- 获取当前时间前一个月第一天
- @return String
*/
public static String getBeforeMonthFirstDay() {
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.MONTH, -1);
calendar1.set(Calendar.DAY_OF_MONTH,1);
String firstDay = sdf.format(calendar1.getTime());
return firstDay;
}
/**
- 获取前一个月最后一天
- @return String
*/
public static String getBeforeMonthLastDay() {
//获取前一个月最后一天
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH, 0);
String lastDay = sdf.format(calendar2.getTime());
return lastDay;
}
- 时间格式(yyyy-MM-dd)
}