package com.ahpu.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 时间类的工具类
*
*
*/
public class DateUtil {
private static final TimeZone timeZone = TimeZone.getTimeZone("GMT+08:00");
private static Log log = LogFactory.getLog(DateUtil.class);
/**
* 获取现在的年月
*
*/
public static String getTimeNY() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM");
return sf.format(new Date());
}
public static String getTimeSSS() {
SimpleDateFormat sdf = new SimpleDateFormat("SSS");
String format = sdf.format(new Date());
return format;
}
public static String dateToString(@NotNull Date date) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
}
public static String dateToString(@NotNull String template, @NotNull Date date) {
return new SimpleDateFormat(template).format(date);
}
/**
* 获取两个时间中的所有时间
*
*/
public static List<String> getBetweenDates(String dateStart, String dataEnd) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date begin = null;
Date end = null;
try {
begin = sdf.parse(dateStart);
end = sdf.parse(dataEnd);
} catch (ParseException e) {
log.error(e.toString());
}
List<String> result = new ArrayList<String>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
while (begin.getTime() <= end.getTime()) {
result.add(sdf.format(tempStart.getTime()));
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
}
return result;
}
public static String thisTime() {
SimpleDateFormat fs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return fs.format(new Date());
}
/**
* 将时间格式化为指定格式
*
*/
public static String formatDefinition(String time, String format) {
SimpleDateFormat fs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
SimpleDateFormat f = new SimpleDateFormat(format);
Date date;
try {
date = new Date(fs.parse(time).getTime());
} catch (ParseException e) {
try {
f.parse(time);
return time;
} catch (ParseException e1) {
return "FormatError";
}
}
return f.format(date);
}
/**
* 将时间格式化为指定格式
*
*/
public static String formatDefinition(String time, String format, String zdyFormat) {
SimpleDateFormat fs = new SimpleDateFormat(zdyFormat);
SimpleDateFormat f = new SimpleDateFormat(format);
Date date;
try {
date = new Date(fs.parse(time).getTime());
} catch (ParseException e) {
try {
f.parse(time);
return time;
} catch (ParseException e1) {
return "FormatError";
}
}
return f.format(date);
}
/**
* 把yyyy-MM-dd HH:mm:ss.S格式的时候格式化为yyyy-MM-dd HH:mm:ss格式
*
*/
public static String formatDeleteSecond(String time) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat fs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Date date;
try {
date = new Date(fs.parse(time).getTime());
} catch (ParseException e) {
try {
f.parse(time);
return time;
} catch (ParseException e1) {
return "Error";
}
}
return f.format(date);
}
/**
* @param day 推移的天数
* @return date时间加天数的日期
*/
public static String addDate(String date, int day) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(getDate(date, "yyyy-MM-dd"));
calendar.add(GregorianCalendar.DAY_OF_MONTH, day);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(calendar.getTime());
}
/**
* @param date 当前日期值
* @param year 推移的年数
* @param formatStr 返回的格式
* @return date时间加天数的日期
* @author Mengfw
*/
public static String addYear(String date, int year, String formatStr) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(getDate(date, "yyyy-MM-dd"));
calendar.add(GregorianCalendar.YEAR, year);
SimpleDateFormat format = new SimpleDateFormat(formatStr);
return format.format(calendar.getTime());
}
public static String addDate2(String date, int day) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(getDate(date, "yyyyMMdd"));
calendar.add(GregorianCalendar.DAY_OF_MONTH, day);
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(calendar.getTime());
}
public static String addWeek(String date, int week) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(getDate(date, "yyyy-MM-dd"));
calendar.add(GregorianCalendar.WEEK_OF_YEAR, week);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(calendar.getTime());
}
public static String addMonth(String date, int month) {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(getDate(date, "yyyy-MM-dd"));
calendar.add(GregorianCalendar.MONTH, month);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(calendar.getTime());
}
/**
* 日期转换成字符串 把长日期 yyyy-MM-dd HH:mm 转换成短日期格式 yyyy-MM-dd
*/
public static String dateToString(String StrDate) {
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd");
String dd = d.format(isToDate(StrDate));
return dd;
}
/**
*
* 日期转换成字符串 把长日期 yyyy-MM-dd HH:mm 转换成短日期格式 yyyy-MM-dd HH:mm:ss
*/
public static String dateToStringSecond(String strDate) {
SimpleDateFormat d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date;
String dd;
try {
date = d.parse(strDate);
dd = d.format(date);
return dd;
} catch (Exception e) {
log.error(e.toString());
return "";
}
}
public static synchronized String formatDate(int dateNum) {
String result = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
try {
Date date = formatter.parse(String.valueOf(dateNum));
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
result = formatter1.format(date);
} catch (Exception e) {
}
return result;
}
public static synchronized int formatDate(Date date) {
String result = "0";
try {
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
result = formatter1.format(date);
} catch (Exception e) {
}
return Integer.parseInt(result);
}
/**
* @param msel 毫秒时间
* @return 毫秒时间 所对应的"yyyy-MM-dd HH:mm"格式的日期
*/
public static String formatTime(long msel) {
Date date = new Date(msel);
SimpleDateFormat formatter = new SimpleDateFor