import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.time.DateFormatUtils;
/**
* 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
* @author Lion
* @version 2014-4-15
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
private static Map<String, ThreadLocal<SimpleDateFormat>> formatMap = new HashMap<>();
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"};
/**
* 得到当前日期字符串 格式(yyyy-MM-dd)
*/
public static String getDate() {
return getDate("yyyy-MM-dd");
}
/**
* 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String getDate(String pattern) {
return DateFormatUtils.format(new Date(), pattern);
}
/**
* 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
*/
public static String formatDate(Date date, Object... pattern) {
String formatDate = null;
if (pattern != null && pattern.length > 0) {
formatDate = DateFormatUtils.format(date, pattern[0].toString());
} else {
formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
}
return formatDate;
}
/**
* 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
*/
public static String formatDateTime(Date date) {
return formatDate(date, "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前时间字符串 格式(HH:mm:ss)
*/
public static String getTime() {
return formatDate(new Date(), "HH:mm:ss");
}
/**
* 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
*/
public static String getDateTime() {
return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 得到当前年份字符串 格式(yyyy)
*/
public static String getYear() {
return formatDate(new Date(), "yyyy");
}
/**
* 得到当前月份字符串 格式(MM)
*/
public static String getMonth() {
return formatDate(new Date(), "MM");
}
/**
* 得到当天字符串 格式(dd)
*/
public static String getDay() {
return formatDate(new Date(), "dd");
}
/**
* 得到当前星期字符串 格式(E)星期几
*/
public static String getWeek() {
return formatDate(new Date(), "E");
}
/**
*@param datetime为空则默认今天日期
*@return 返回0是星期日、1是星期一、2是星期二、3是星期三、4是星期四、5是星期五、6是星期六
*/
public static int getDayofweek(Long datetime){
Calendar cal = Calendar.getInstance();
if(datetime==null){
cal.setTime(new Date(System.currentTimeMillis()));
}else{
cal.setTime(new Date(datetime));
}
return cal.get(Calendar.DAY_OF_WEEK)-1;
}
/**
* 日期型字符串转化为日期 格式
* { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
* "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
* "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
*/
public static Date parseDate(Object str) {
if (str == null){
return null;
}
try {
return parseDate(str.toString(), parsePatterns);
} catch (ParseException e) {
return null;
}
}
/**
* 获取过去的天数
* @param date
* @return
*/
public static long pastDays(Date date) {
long t = new Date().getTime()-date.getTime();
return t/(24*60*60*1000);
}
/**
* 获取过去的小时
* @param date
* @return
*/
public static long pastHour(Date date) {
long t = new Date().getTime()-date.getTime();
return t/(60*60*1000);
}
/**
* 获取过去的分钟
* @param date
* @return
*/
public static long pastMinutes(Date date) {
long t = new Date().getTime()-date.getTime();
return t/(60*1000);
}
/**
* 转换为时间(天,时:分:秒.毫秒)
* @param timeMillis
* @return
*/
public static String formatDateTime(long timeMillis){
long day = timeMillis/(24*60*60*1000);
long hour = (timeMillis/(60*60*1000)-day*24);
long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
}
/**
* 获取两个日期之间的天数
*
* @param before
* @param after
* @return
*/
public static double getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
/**
*
* @param type(1:年;2:月,5:日)
* @param num 差值
* @return
*/
public static Date getCalcBeforeDate(int type, int num ,Date date){
date = date == null ? new Date() : date;//获取当前时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(type, num);//当前时间减去一年,即一年前的时间
return calendar.getTime();
}
/**
* 获取当前指定时间的前后时间点
* @param execDate
* @param num
* @param unit
* @return
*/
public static Date getPreDateByNum(Date execDate, int num, String unit) {
Date date = null;
switch (unit) {
case "minute": //分钟
date = DateUtils.getCalcBeforeDate(Calendar.MINUTE, num, execDate);
break;
case "hour": //小时
date = DateUtils.getCalcBeforeDate(Calendar.HOUR, num, execDate);
break;
case "day": //天
date = DateUtils.getCalcBeforeDate(Calendar.DATE, num, execDate);
break;
case "week": //周
date = DateUtils.getCalcBeforeDate(Calendar.WEEK_OF_YEAR, num, execDate);
break;
case "month": //月
date = DateUtils.getCalcBeforeDate(Calendar.MONTH, num, execDate);
break;
case "year": //年
date = DateUtils.getCalcBeforeDate(Calendar.YEAR, num, execDate);
break;
default:
break;
}
return date;
}
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
// System.out.println(formatDate(parseDate("2010/3/6")));
// System.out.println(getDate("yyyy年MM月dd日 E"));
// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
// System.out.println(time/(24*60*60*1000));
String tableName = "out_table_008";
String sufix = tableName.substring(tableName.lastIndexOf("_")+1,tableName.length());
System.out.println(sufix);
int num = 1;
String str = DateUtils.formatDate(getPreDateByNum(new Date(), -num, "day"), "yyyMMdd");
System.out.println(str);
System.err.println(getDayofweek(null));
}
/**
* 获取模板时间
*
* @param date
* 时间
* @param dateEnum
* 日期枚举模板
* @return
*/
public static String format(Date date, String dateEnum) {
if (date == null) {
return "";
}
return SimpleDate(dateEnum).format(date);
}
/**
* 获取时间模型
*
* @param dateEnum
* @return
*/
private static SimpleDateFormat SimpleDate(final String dateEnum) {
ThreadLocal<SimpleDateFormat> thread = formatMap.get(dateEnum);
if (thread == null) {
synchronized (DateUtils.class) {
thread = formatMap.get(dateEnum);
if (thread == null) {
thread = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(dateEnum);
}
};
formatMap.put(dateEnum, thread);
}
}
}
return thread.get();
}
/**
* 字符串时间转LONG
* @param sdate
* @return
*/
public static long string2long(String sdate){
if(sdate.length() < 11){
sdate = sdate + " 00:00:00";
}
SimpleDateFormat sdf= new SimpleDateFormat(DEFAULT_PATTERN);
Date dt2 = null;
try {
dt2 = sdf.parse(sdate);
} catch (ParseException e) {
e.printStackTrace();
}
//继续转换得到秒数的long型
long lTime = dt2.getTime() / 1000;
return lTime;
}
/**
* LONG时间转字符串
* @param ldate
* @return
*/
public static String long2string(long ldate){
SimpleDateFormat sdf= new SimpleDateFormat(DEFAULT_PATTERN);
//前面的ldate是秒数,先乘1000得到毫秒数,再转为java.util.Date类型
java.util.Date dt = new Date(ldate * 1000);
String sDateTime = sdf.format(dt); //得到精确到秒的表示
if(sDateTime.endsWith("00:00:00")){
sDateTime = sDateTime.substring(0,10);
}
return sDateTime;
}
}
dataUtil
最新推荐文章于 2020-10-25 19:36:05 发布