package cn.ce.ebiz.framework.util.common.date;
import org.apache.commons.lang3.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @类功能说明: <图片库> 日期工具类
* * @公司名称 :
* * @作者 admin
* * @创建时间 :Jul 31, 2014 4:20:17 PM
* @版本 :V1.0
*/
public class DateUtil {
private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
/**
* @param n
* @return
* @throws ParseException
* @方法功能说明:当前时间的最近n天
* @作者 :
* @创建时间 :Jul 31, 2014 3:39:01 PM
*/
public static ModelDate getLastThreeDays(int n) throws ParseException {
n -= 1;
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -n);
String startDate = returnDetail(threadLocal.get().format(calendar.getTime()), 0);
String endDate = returnDetail(threadLocal.get().format(new Date()), 1);
return new ModelDate(startDate, endDate);
}
/**
* @return
* @throws ParseException
* @方法功能说明:获取本周 周一和周日
* @作者 :
* @创建时间 :Jul 31, 2014 5:32:53 PM
*/
public static ModelDate getThisWeekDays() throws ParseException {
String startDate = returnDetail(threadLocal.get().format(getDayOfWeek(1)), 0);
String endDate = returnDetail(threadLocal.get().format(getDayOfWeek(7)), 1);
return new ModelDate(startDate, endDate);
}
/**
* @return
* @throws ParseException
* @方法功能说明:获取本月第一天和最后一天
* @作者 :
* @创建时间 :Aug 1, 2014 2:14:37 PM
*/
public static ModelDate getThisMonth() throws ParseException {
String startDate = returnDetail(threadLocal.get().format(getFirstDayOfMonth()), 0);
String endDate = returnDetail(threadLocal.get().format(getLastDayOfMonth()), 1);
return new ModelDate(startDate, endDate);
}
/**
* @param n
* @return
* @方法功能说明:本周的第n天
* @作者 :
* @创建时间 :Jul 31, 2014 5:34:44 PM
*/
private static Date getDayOfWeek(int n) {
Calendar c = Calendar.getInstance();
int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
if (day_of_week == 0) {
day_of_week = 7;
}
c.add(Calendar.DATE, -day_of_week + n);
return c.getTime();
}
/**
* @return
* @方法功能说明:本月第一天
* @作者 :
* @创建时间 :Aug 1, 2014 2:11:49 PM
*/
private static Date getFirstDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
* @return
* @方法功能说明:本月最后一天
* @作者 :
* @创建时间 :Aug 1, 2014 2:14:54 PM
*/
public static Date getLastDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
* @param source
* @param type
* @return
* @方法功能说明:
* @作者 :
* @创建时间 :Aug 1, 2014 3:27:34 PM
*/
public static String returnDetail(String source, int type) {
return StringUtils.isEmpty(source) ? null : (source + (type == 0 ? " 00:00:00" : " 23:59:59"));
}
/**
* @param date
* @return
* @方法功能说明:
* @作者 :
* @创建时间 :Sep 18, 2014 2:03:11 PM
*/
public static String messpostDate(Date date) {
if (date == null) {
return null;
}
Calendar calendar = Calendar.getInstance();
String now = threadLocal.get().format(calendar.getTime());
String target = threadLocal.get().format(date);
long targetTime = date.getTime();
long nowTime = calendar.getTime().getTime();
if (now.equals(target)) {//同一天
long time = nowTime - targetTime;
if (time < 3600000) {
long min = time / 60000;
//2014-09-24 产品要求 0分钟前 显示为1分钟前
return (min <= 0 ? 1 : min) + "分钟前";
} else {
return (time / 3600000) + "小时前";
}
}
return recurrenceDate(target, now, 0, calendar);
}
/**
* @param target
* @param now
* @param i
* @param calendar
* @return
* @方法功能说明:计算数据日期距离当前日期 时间
* @作者 :
* @创建时间 :Sep 19, 2014 10:53:46 AM
*/
public static String recurrenceDate(String target, String now, int i, Calendar calendar) {
if (target == null) {
return null;
}
if (i > 3) {
return null;
}
i++;
String result = null;
calendar.add(Calendar.DATE, -1);
now = threadLocal.get().format(calendar.getTime());
if (now.equals(target)) {
switch (i) {
case 1:
result = "昨天";
break;
case 2:
result = "前天";
break;
case 3:
result = "前三天";
break;
}
return result;
} else {
return recurrenceDate(target, now, i, calendar);
}
}
/**
* @param mills
* @return
* @方法功能说明: 将秒换算成 00:00:00 格式的时间(小时不限制,不足两位数补齐两位,两位或以上直接显示)
* @作者 :
* @创建时间 :2015-7-3 下午5:04:48
*/
public static String getHMSTimeStringByMills(int mills) {
if (mills <= 0) {
return "00:00:00";
}
int hour = 0;
int miniute = 0;
int mill = 0;
if (mills < 60) {
mill = mills;
return getHMSTimeStringByHMS(hour, miniute, mill);
} else {
miniute = mills / 60;
mill = mills % 60;
if (miniute < 60) {
return getHMSTimeStringByHMS(hour, miniute, mill);
} else {
hour = miniute / 60;
miniute = miniute % 60;
return getHMSTimeStringByHMS(hour, miniute, mill);
}
}
}
private static String getHMSTimeStringByHMS(int hour, int miniute, int mill) {
StringBuilder timeSb = new StringBuilder();
String hourStr = getFormatTimeStr(hour);
String minuteStr = getFormatTimeStr(miniute);
String millStr = getFormatTimeStr(mill);
timeSb.append(hourStr).append(":");
timeSb.append(minuteStr).append(":");
timeSb.append(millStr);
return timeSb.toString();
}
private static String getFormatTimeStr(int time) {
String str = String.valueOf(time);
if (String.valueOf(time).length() < 2) {
str = "0" + time;
}
return str;
}
}
package cn.ce.ebiz.framework.util.common.date;
public class ModelDate {
private String startDate;
private String endDate;
public ModelDate() {
super();
}
public ModelDate(String startDate, String endDate) {
super();
this.startDate = startDate;
this.endDate = endDate;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
}
DateUtil日期操作
最新推荐文章于 2024-08-20 23:53:43 发布