package com.xtgd.main.util;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.apache.commons.lang.StringUtils;
/**
* 日期工具类
*
* @author yugc
*/
public final class DateUtil {
private DateUtil() {
}
/**
* yyyy-MM-dd
*/
public static final String FORMAT1
= "yyyy-MM-dd";
/**
* yyyy.MM.dd
*/
public static final String FORMAT2
= "yyyy.MM.dd";
/**
* yyyy/MM/dd
*/
public static final String FORMAT3
= "yyyy/MM/dd";
/**
* yyyy-MM-dd HH:mm
*/
public static final String FORMAT4
= "yyyy-MM-dd HH:mm";
/**
* yyyy.MM.dd HH:mm
*/
public static final String FORMAT5
= "yyyy.MM.dd HH:mm";
/**
* yyyy/MM/dd HH:mm
*/
public static final String FORMAT6
= "yyyy/MM/dd HH:mm";
/**
* yyyy-MM-dd HH:mm:ss
*/
public static final String FORMAT7
= "yyyy-MM-dd HH:mm:ss";
/**
* YYYY-MM-dd HH-mm-ss
*/
public static final String FORMAT15
= "YYYY-MM-dd HH-mm-ss";
/**
* yyyy.MM.dd HH:mm:ss
*/
public static final String FORMAT8
= "yyyy.MM.dd HH:mm:ss";
/**
* yyyy/MM/dd HH:mm:ss
*/
public static final String FORMAT9
= "yyyy/MM/dd HH:mm:ss";
/**
* yyyy_MM_dd_HH_mm_ss
*/
public static final String FORMAT10
= "yyyy_MM_dd_HH_mm_ss";
/**
* yy-MM-dd
*/
public static final String FORMAT11
= "yy-MM-dd";
/**
* yyyyMMdd
*/
public static final String FORMAT12
= "yyyyMMdd";
/**
* yyyyMMddHHmmss
*/
public static final String FORMAT13
= "yyyyMMddHHmmss";
/**
* yyyyMM
*/
public static final String FORMAT14
= "yyyyMM";
public static Date getCurrentDate
() {
}
public static Date getYesterDay
() {
return addDay
(new Date(System.
currentTimeMillis()),
-1);
}
public static String getYesterDayString
() {
return parseDateToString
(addDay
(new Date(System.
currentTimeMillis()),
-1), FORMAT1
);
}
/**
* 得到年月日的路径
* @return
*/
public static String getThisYearMonthDay
(String dateString
) {
Date date
= parseStringToDate
(dateString, FORMAT12
);
return DateUtil.getYear(date) + "/" + DateUtil.getMonth(date) + "/" + DateUtil.getDay(date) + "/";
}
/**
* 返回当前日期或时间
*
* @param format
* @return
*/
if (StringUtils.isBlank(format)) {
format = FORMAT1;
}
String currentTime
= formatter.
format(date
);
return currentTime;
}
/**
* 返回当前时间
*
* @return
*/
public static String getCurrentTime
() {
String datetime
= getCurrentDate
(FORMAT7
);
String time
= datetime.
substring(datetime.
indexOf(" ") + 1);
return time;
}
/**
* 返回当前时间加随机数
*
* @return
*/
public static String getCurrentDateTimeRandom
() {
String datetime
= getCurrentDate
(DateUtil.
FORMAT10) + "_" + Math.
random();
return datetime;
}
/**
* 返回当前时间和日期
* @return
*/
public static String getCurrentDateTimeString
() {
return getCurrentDate(DateUtil.FORMAT7);
}
/**
* 返回当前日期
*
* @return
*/
public static Date getCurrentDateTime
() {
String datetime
= getCurrentDate
(FORMAT7
);
return parseStringToDate(datetime, "");
}
public static Timestamp getCurrentTimestamp
() {
String datetime
= getCurrentDate
(FORMAT7
);
return parseStringToTimestamp(datetime, "");
}
String datetime
= getCurrentDate
(format
);
return parseStringToTimestamp(datetime, "");
}
/**
* 日期转换为字符串
*
* @param date 日期
* @param format 格式
* @return 返回字符型日期
*/
try {
if (date != null) {
if (StringUtils.isBlank(format)) {
}
else {
}
result = formatter.format(date);
}
}
}
return result;
}
/**
* 时间1-时间2的毫秒
*
* @param t1
* @param t2
* @return
*/
if ((t1 != null) && (t2 != null)) {
return t1.getTime() - t2.getTime();
}
return 0;
}
/**
* 两个日期date1-date2相减,相差的天数
*
* @param date1
* 日期
* @param date2
* 日期
* @return 返回相减后的日期
*/
public static int betweenTwoDates
(Date date1,
Date date2
) {
return (int) ((getMillis(date1) - getMillis(date2)) / (24 * 3600 * 1000));
}
/**
* 将字符串转换为日期
*
* @param str
* @return
* @throws ParseException
*/
if (StringUtils.isNotBlank(str)) {
if (StringUtils.isBlank(format)) {
}
else {
}
try {
date = formatter.parse(str);
}
e.printStackTrace();
}
}
return date;
}
/**
* 返回2007到今年的年份
*
* @return
*/
public static List<Integer> get2007ToThisYear() {
// 当前时间
// 返回的年份
List<Integer> the2007ToThisYearList = new ArrayList<Integer>();
// 当前年
for (int i = thisYear; i >= 2007; i--) {
the2007ToThisYearList.add(i);
}
return the2007ToThisYearList;
}
/**
* 获取当前月的前几个月份的时间
* @param months
* @return
*/
public static Date getDateBeforeMonths
(int months
) {
// 当前时间
return c.getTime();
}
/**
* 获取当前日期的前几天的时间
* @param days
* @return
*/
public static Date getDateBeforeDays
(int days
) {
// 当前时间
return c.getTime();
}
/**
* 返回1-12月份
*
* @return
*/
public static List<String> getAllMonth() {
List<String> theMonthList = new ArrayList<String>();
for (int i = 1; i < 13; i++) {
if (i < 10) {
theMonthList.add("0" + i);
}
else {
theMonthList.add("" + i);
}
}
return theMonthList;
}
/**
* 返回日期中的年份
*
* @param date
* 日期
* @return 返回年份
*/
public static int getYear
(Date date
) {
c.setTime(date);
}
/**
* 返回日期中的月份
*
* @param date
* 日期
* @return 返回月份
*/
public static int getMonth
(Date date
) {
c.setTime(date);
}
/**
* 返回日期中的日
*
* @param date
* 日期
* @return 返回日
*/
public static int getDay
(Date date
) {
c.setTime(date);
}
/**
* 返回日期中的小时
*
* @param date
* 日期
* @return 返回小时
*/
public static int getHour
(Date date
) {
c.setTime(date);
}
/**
* 返回日期中的分钟
*
* @param date
* 日期
* @return 返回分钟
*/
public static int getMinute
(Date date
) {
c.setTime(date);
}
/**
* 返回日期中的秒钟
*
* @param date
* 日期
* @return 返回秒钟
*/
public static int getSecond
(Date date
) {
c.setTime(date);
}
/**
* 返回日期代表的毫秒
*
* @param date
* 日期
* @return 返回毫秒
*/
public static long getMillis
(Date date
) {
c.setTime(date);
return c.getTimeInMillis();
}
/**
* 返回当前日期代表的毫秒
*
* @return
*/
public static long getCurrentTimeMillis() {
return System.
currentTimeMillis();
}
public static Date addMonth
(Date date,
int month
) {
c.setTime(date);
c.add(c.MONTH, month);
return c.getTime();
}
/**
* 日期加天数,可以向前加,向后加
*
* @param date
* 日期
* @param day
* 天数
* @return 返回相加后的日期
*/
public static Date addDay
(Date date,
int day
) {
c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
return c.getTime();
}
/**
* 日期加秒数,可以向前加,向后加
*
* @param date
* 日期
* @param second
* 秒数
* @return 返回相加后的日期
*/
public static Date addSecond
(Date date,
long second
) {
c.setTimeInMillis(getMillis(date) + second * 1000);
return c.getTime();
}
/**
* 根据一个日期,返回是星期几
*
* @param sdate
* @return
*/
// 再转换为时间
Date date
= parseStringToDate
(sdate,
"");
c.setTime(date);
// day中存的就是星期几了,其范围 1~7
// 1=星期日 7=星期六,其他类推
return day + "";
}
/**
* 根据一个日期,返回是星期几
* 1=星期日 7=星期六,其他类推
*
* @param sdate
* @return
*/
public static int getWeekByDate
(Date date
) {
// 再转换为时间
c.setTime(date);
// day中存的就是星期几了,其范围 1~7
// 1=星期日 7=星期六,其他类推
}
/**
* 日期为一年中的第几周
* @return
*/
c.setTime(date);
return week;
}
public static java.
sql.
Date parseUtilDateToSqlDate
(Date date
) {
if (date != null) {
return new java.
sql.
Date(date.
getTime());
}
else {
return null;
}
}
public static java.
sql.
Date parseStringToSqlDate
(String dateStr,
String format
) {
if (StringUtils.isBlank(format)) {
date = parseStringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
}
else {
date = parseStringToDate(dateStr, format);
}
return parseUtilDateToSqlDate(date);
}
return parseStringToTimestamp(parseDateToString(date, format), format);
}
return parseStringToDate(parseDateToString(date, format), format);
}
if (StringUtils.isBlank(dateStr)) {
return null;
}
if (StringUtils.isBlank(format)) {
date = parseStringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
}
else {
date = parseStringToDate(dateStr, format);
}
if (date != null) {
long t = date.getTime();
}
else {
return null;
}
}
/**
* 获取时间2099-12-31,23:59:59
*
* @return
*/
return DateUtil.parseStringToTimestamp("2099-12-31 23:59:59", DateUtil.FORMAT7);
}
/**
* 返回日期中的年月日,格式化成yyyyMMdd
* @param date
* @return
*/
String currentTime
= formatter.
format(date
);
return currentTime;
}
/**
* 取得指定月份的第一天
*
* @param strdate String
* @return String
*/
return parseDateToString(parseStringToDate(strdate, "yyyy-MM"), "");
}
/**
* 取得指定月份的最后一天
*
* @param strdate String
* @return String
*/
Date date
= parseStringToDate
(getMonthBegin
(strdate
),
"");
calendar.setTime(date);
return parseDateToString(calendar.getTime(), "");
}
public static String getPreviousMonthBegin
() {
Date date
= new Date(cal.
getTimeInMillis());
return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT1);
}
public static String getPreviousMonthEnd
() {
Date date
= parseStringToDate
(getPreviousMonthBegin
(), FORMAT1
);
calendar.setTime(date);
return parseDateToString(calendar.getTime(), FORMAT1);
}
/**
* 上个月
*
* @return
*/
public static String getPreviousMonth
() {
Date date
= new Date(cal.
getTimeInMillis());
return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT14);
}
/**
* 上两个月
*
* @return
*/
public static String getPreviousTwoMonth
() {
Date date
= new Date(cal.
getTimeInMillis());
return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT14);
}
/**
* 判断两个日期是否在同一周
* @param date1
* @param date2
* @return
*/
public static boolean isSameWeekDates
(Date date1,
Date date2
) {
cal1.setTime(date1);
cal2.setTime(date2);
if (0 == subYear) {
return true;
}
}
else if ((1 == subYear
) && (11 == cal2.
get(Calendar.
MONTH))) {
// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
return true;
}
}
else if ((-1 == subYear
) && (11 == cal1.
get(Calendar.
MONTH))) {
return true;
}
}
return false;
}
public static void main
(String[] args
) {
System.
out.
println(getPreviousMonthBegin
());
System.
out.
println(getPreviousMonthEnd
());
System.
out.
println(getYearMonthDay
(parseStringToDate
("2009-11-2 12:1:21", FORMAT7
)));
System.
out.
println("current time: " + getCurrentDateTime
());
System.
out.
println("addsecond: " + addSecond
(getCurrentDateTime
(),
-1L
));
System.
out.
println("current date: " + date.
toString());
System.
out.
println("test parseDateToString: " + parseDateToString
(date,
""));
System.
out.
println("test parseStringToDate: " + parseStringToDate
("1990-01-1 00:00:00",
""));
System.
out.
println("test getYear: " + getYear
(date
));
System.
out.
println("test getMonth: " + getMonth
(date
));
System.
out.
println("test getDay: " + getDay
(date
));
System.
out.
println("test getHour: " + getHour
(date
));
System.
out.
println("test getMinute: " + getMinute
(date
));
System.
out.
println("test getSecond: " + getSecond
(date
));
System.
out.
println("test getMillis: " + getMillis
(date
));
System.
out.
println("test addDate: " + addDay
(date,
2));
System.
out.
println("test betweenTwoDays: " + betweenTwoDates
(date, addDay
(date,
2)));
System.
out.
println("test getWeekNum: " + getWeekNum
(addDay
(date,
-2)));
System.
out.
println("test getWeekByDate: " + getWeekByDate
(parseDateToString
(date,
"")));
System.
out.
println("test getMonthBegin: " + getMonthBegin
(parseDateToString
(date,
"")));
System.
out.
println("test getMonthEnd: " + getMonthEnd
(parseDateToString
(date,
"")));
System.
out.
println("test isSameWeekDates: " + isSameWeekDates
(date, addDay
(date,
-2)));
System.
out.
println(getPreviousTwoMonth
());
System.
out.
println(getPreviousMonth
());
List<Integer> yearList = get2007ToThisYear();
List<String> monthList = getAllMonth();
}
if(stopTime != null && !"".equals(stopTime))
{
if(stopTime.length() < 19)
{
finishTime = stopTime.substring(0, 10) + " 00:00:00";
}
finishDate = DateUtil.parseStringToDate(finishTime, DateUtil.FORMAT7);
finishDate = DateUtil.addDay(finishDate, 1);
return DateUtil.parseDateToString(finishDate, DateUtil.FORMAT7);
}else{
return null;
}
}
/**
* 给date加1天
* @param date
* @return
*/
public static Date addOneDay
(Date date
) {
calendar.setTime(date);
return calendar.getTime();
}
}
//java/6806