DateFilter

package org.sidao.common;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFilter {

private static final String DATEFORMAT = "yyyy-MM-dd";
private static final String TIMEFORMAT = "HH:mm:ss";
private static final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String YYYY = "yyyy";
private static final String MM = "MM";
private static final String DD = "dd";
private static final String HH = "HH";
private static final String MI = "mm";
private static final String SS = "ss";

/**
* 获取当前日期
*
* @return Date
*/
public static Date getDate() {
return Calendar.getInstance().getTime();
}

/**
* 获取当前日期 格式:yyyy-MM-dd
*
* @return String
*/
public static String getCurrentDate() {
return formatDate(getDate(), DATEFORMAT);
}

/**
* 获取当前时间 格式:HH:mm:ss
*
* @return String
*/
public static String getCurrentTime() {
return formatDate(getDate(), TIMEFORMAT);
}

/**
* 获取当前日期 格式:yyyy-MM-dd HH:mm:ss
*
* @return String
*/
public static String getCurrentDateTime() {
return formatDate(getDate(), DATETIMEFORMAT);
}

/**
* 格式化指定日期
*
* @param date -
* 日期型
* @param formatStr -
* 日期格式化字符串 如:yyyy-MM-dd等
* @return String
*/
public static String formatDate(Date date, String formatStr) {
SimpleDateFormat bdf = new SimpleDateFormat(formatStr);
return bdf.format(date);
}

/**
* 格式化指定日期,格式:yyyy-MM-dd
*
* @param date
* @return
*/
public static String withDateFormat(Date date) {
String dateStr = "";
if (date != null)
dateStr = formatDate(date, DATEFORMAT);
return dateStr;
}

/**
* 格式化指定时间,格式:HH:mm:ss
*
* @param date
* @return
*/
public static String withTimeFormat(Date date) {
String dateStr = "";
if (date != null)
dateStr = formatDate(date, TIMEFORMAT);
return dateStr;
}

/**
* 格式化指定日期和时间,格式:yyyy-MM-dd HH:mm:ss
*
* @param date
* @return
*/
public static String withDateTimeFormat(Date date) {
String dateStr = "";
if (date != null)
dateStr = formatDate(date, DATETIMEFORMAT);
return dateStr;
}

/**
* 获取几天前的日期
*
* @param dayCount -
* 天数:正数
* @return Date
*/
public static Date getBeforeDayDate(int dayCount) {
Date date = null;
if (dayCount > 0) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -dayCount);
date = calendar.getTime();
}
return date;
}

/**
* 获取几天前的日期 格式:yyyy-MM-dd
*
* @param dayCount -
* 天数:正数
* @return String
*/
public static String getBeforeDayDateStr(int dayCount) {
String dateStr = "";
if (dayCount > 0)
dateStr = formatDate(getBeforeDayDate(dayCount), DATEFORMAT);
return dateStr;
}

/**
* 获取几天后的日期
*
* @param dayCount -
* 天数:正数
* @return Date
*/
public static Date getAfterDayDate(int dayCount) {
Date date = null;
if (dayCount > 0) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, dayCount);
date = calendar.getTime();
}
return date;
}

/**
* 获取几天后的日期 格式:yyyy-MM-dd
*
* @param dayCount -
* 天数:正数
* @return String
*/
public static String getAfterDayDateStr(int dayCount) {
String dateStr = "";
if (dayCount > 0)
dateStr = formatDate(getAfterDayDate(dayCount), DATEFORMAT);
return dateStr;
}

/**
* 获取几个月前的日期
*
* @param monthCount -
* 天数:正数
* @return Date
*/
public static Date getBeforeMonthDate(int monthCount) {
Date date = null;
if (monthCount > 0) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -monthCount);
date = calendar.getTime();
}
return date;
}

/**
* 获取几个月前的日期 格式:yyyy-MM-dd
*
* @param monthCount -
* 天数:正数
* @return String
*/
public static String getBeforeMonthDateStr(int monthCount) {
String dateStr = "";
if (monthCount > 0)
dateStr = formatDate(getBeforeMonthDate(monthCount), DATEFORMAT);
return dateStr;
}

/**
* 获取几个月后的日期
*
* @param monthCount -
* 天数:正数
* @return Date
*/
public static Date getAfterMonthDate(int monthCount) {
Date date = null;
if (monthCount > 0) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, monthCount);
date = calendar.getTime();
}
return date;
}

/**
* 获取几个月后的日期 格式:yyyy-MM-dd
*
* @param monthCount -
* 天数:正数
* @return String
*/
public static String getAfterMonthDateStr(int monthCount) {
String dateStr = "";
if (monthCount > 0)
dateStr = formatDate(getAfterMonthDate(monthCount), DATEFORMAT);
return dateStr;
}

/**
* 将日期字符串转换成日期型
*
* @param dateStr -
* 日期字符串,格式:yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss。
* 但只格式化日期部分,不格式化时间部分,被置为yyyy-MM-dd 00:00:00
* @return Date
*/
public static Date str2Date(String dateStr) {
Date date = null;
try {
if (!"".equals(dateStr) && null != dateStr) {
date = DateFormat.getDateInstance().parse(dateStr);
}
} catch (ParseException e) {
throw new RuntimeException("日期格式化出错!");
}
return date;
}

/**
* 将时间字符串转换成日期型
*
* @param timeStr -
* 时间字符串,必须是时间格式:HH:mm:ss
* @return Date
*/
public static Date str2Time(String timeStr) {
Date date = null;
try {
if (!"".equals(timeStr) && null != timeStr) {
date = DateFormat.getTimeInstance().parse(timeStr);
}
} catch (ParseException e) {
throw new RuntimeException("时间格式化出错!时间字符串,正确格式:HH:mm:ss。");
}
return date;
}

/**
* 将日期时间字符串转换成日期型
*
* @param dateTimeStr -
* 日期时间字符串,必须是日期时间格式:yyyy-MM-dd HH:mm:ss。
* @return Date
*/
public static Date str2DateTime(String dateTimeStr) {
Date date = null;
try {
if (!"".equals(dateTimeStr) && null != dateTimeStr) {
date = DateFormat.getDateTimeInstance().parse(dateTimeStr);
}
} catch (ParseException e) {
throw new RuntimeException(
"日期格式化出错!日期时间字符串,正确格式:yyyy-MM-dd HH:mm:ss。");
}
return date;
}

/**
* 将日期型转换成日期字符串
*
* @param date -
* 指定日期
* @return String
*/
public static String date2Str(Date date) {
String dateStr = "";
if (date != null)
dateStr = date.toString();
return dateStr;
}

/**
* 将时间字符串转换成时间
*
* @param hour -
* 小时
* @param minute -
* 分钟
* @param second -
* 秒
* @return
*/
public static Date str2Time(int hour, int minute, int second) {
Date time = null;
try {
Calendar cld = Calendar.getInstance();
cld.set(Calendar.HOUR_OF_DAY, hour);
cld.set(Calendar.MINUTE, minute);
cld.set(Calendar.SECOND, second);
time = cld.getTime();
} catch (Exception e) {
throw new RuntimeException("时间格式化出错!");
}
return time;
}

/**
* 计算两个日期之间的天数,(date2 - date1)
*
* @param date1 -
* 日期型
* @param date2 -
* 日期型
* @param isNegative -
* 是否可以返回负数
* @return long
*/
public static long getDaysFromTwoDate(Date date1, Date date2,
boolean isNegative) {
long days = 0;
long millisecond = date2.getTime() - date1.getTime();
if (!isNegative) {
if (millisecond < 0) {
millisecond = -millisecond;
}
}
days = (millisecond) / (24 * 60 * 60 * 1000);
return days;
}

/**
* 计算两个日期之间的小时差,(date2 - date1)
*
* @param date1 -
* 日期型
* @param date2 -
* 日期型
* @param isNegative -
* 是否可以返回负数
* @return long
*/
public static double getHourFromTwoDate(Date date1, Date date2,
boolean isNegative) {
double days = 0;

long millisecond = date2.getTime() - date1.getTime();
if (!isNegative) {
if (millisecond < 0) {
millisecond = -millisecond;
}
}
days = (millisecond) / (60d * 60d * 1000d);

DecimalFormat df = new DecimalFormat("#.##");
String str = df.format(days);
return Double.parseDouble(str);
}

/**
* 计算两个日期之间的小时差
*
* @param hour1
* @param minute1
* @param second1
* @param hour2
* @param minute2
* @param second2
* @param isNegative -
* 是否可以返回负数
* @return
*/
public static double getHourFromTwoDate(int hour1, int minute1,
int second1, int hour2, int minute2, int second2, boolean isNegative) {
double minutes = 0;
try {
Calendar cld = Calendar.getInstance();
cld.set(Calendar.HOUR_OF_DAY, hour1);
cld.set(Calendar.MINUTE, minute1);
cld.set(Calendar.SECOND, second1);
Date time1 = cld.getTime();

Calendar cld2 = Calendar.getInstance();
cld2.set(Calendar.HOUR_OF_DAY, hour2);
cld2.set(Calendar.MINUTE, minute2);
cld2.set(Calendar.SECOND, second2);
Date time2 = cld2.getTime();

minutes = getHourFromTwoDate(time1, time2, isNegative);
} catch (Exception e) {
throw new RuntimeException("时间格式化出错!");
}
return minutes;
}

/**
* 计算两个日期之间的天数,(dateStr2 - dateStr1)
*
* @param dateStr1 -
* 字符串型
* @param dateStr2 -
* 字符串型
* @param isNegative -
* 是否可以返回负数
* @return long
*/
public static long getDaysFromTwoDate(String dateStr1, String dateStr2,
boolean isNegative) {
return getDaysFromTwoDate(str2Date(dateStr1), str2Date(dateStr2),
isNegative);
}

/**
* 计算两个日期之间的天数,(date - dateStr)
*
* @param dateStr -
* 字符串型
* @param date -
* 日期型
* @param isNegative -
* 是否可以返回负数
* @return long
*/
public static long getDaysFromTwoDate(String dateStr, Date date,
boolean isNegative) {
return getDaysFromTwoDate(str2Date(dateStr), date, isNegative);
}

/**
* 判断是否是上午
*
* @param dateTime
* @return
*/
public static boolean isAM(Date dateTime) {
Calendar cld = Calendar.getInstance();
cld.setTime(dateTime);
int hour = cld.get(Calendar.HOUR_OF_DAY);
if (hour >= 0 && hour < 12) {
return true;
}
return false;
}

/**
* 判断是否是上午
*
* @param hour
* @return
*/
public static boolean isAM(int hour) {
if (hour >= 0 && hour < 12) {
return true;
}
return false;
}

/**
* 判断是否是下午
*
* @param dateTime
* @return
*/
public static boolean isPM(Date dateTime) {
Calendar cld = Calendar.getInstance();
cld.setTime(dateTime);
int hour = cld.get(Calendar.HOUR_OF_DAY);
if (hour >= 12 && hour < 24) {
return true;
}
return false;
}

/**
* 判断是否是下午
*
* @param hour
* @return
*/
public static boolean isPM(int hour) {
if (hour >= 12 && hour < 24) {
return true;
}
return false;
}

/**
* 获取当前日期 4位年
*
* @return
*/
public static String getYYYY() {
return formatDate(getDate(), YYYY);
}

/**
* 获取当前日期 月份
*
* @return
*/
public static String getMM() {
return formatDate(getDate(), MM);
}

/**
* 获取当前日期 天
*
* @return
*/
public static String getDD() {
return formatDate(getDate(), DD);
}

/**
* 获取指定日期 4位年
*
* @param date
* @return
*/
public static String getYYYY(Date date) {
String dateStr = "";
if (date != null)
dateStr = formatDate(date, YYYY);
return dateStr;
}

/**
* 获取指定日期 月份
*
* @param date
* @return
*/
public static String getMM(Date date) {
String dateStr = "";
if (date != null)
dateStr = formatDate(date, MM);
return dateStr;
}

/**
* 获取指定日期 天
*
* @param date
* @return
*/
public static String getDD(Date date) {
String dateStr = "";
if (date != null)
dateStr = formatDate(date, DD);
return dateStr;
}

/**
* 获取当前日期 小时 HH
*
* @return
*/
public static String getHour() {
return formatDate(getDate(), HH);
}

/**
* 获取当前日期 分钟 mm
*
* @return
*/
public static String getMinute() {
return formatDate(getDate(), MI);
}

/**
* 获取当前日期 秒 ss
*
* @return
*/
public static String getSecond() {
return formatDate(getDate(), SS);
}

/**
* 获取指定日期 小时 HH
*
* @param time
* @return
*/
public static String getHour(Date time) {
String timeStr = "";
if (time != null)
timeStr = formatDate(time, HH);
return timeStr;
}

/**
* 获取指定日期 分钟 mm
*
* @param time
* @return
*/
public static String getMinute(Date time) {
String timeStr = "";
if (time != null)
timeStr = formatDate(time, MI);
return timeStr;
}

/**
* 获取指定日期 秒 ss
*
* @param time
* @return
*/
public static String getSecond(Date time) {
String timeStr = "";
if (time != null)
timeStr = formatDate(time, SS);
return timeStr;
}

/**
* 计算从beginDate到endDate的工作日,休息日,自然日(例如要查2个月之间的工作日和休息日)
* ————work_rest_all("2009-03-01", "2009-04-01");
*
* @param beginDate
* 开始时间(包括——如果开始时间为null,默认开始时间为现在)
* @param endDate
* 结束时间(不包括)
* @return [0]工作日,[1]休息日,[2]自然日——总天数
* @throws ParseException
*/
public static int[] work_rest_all(String beginDate, String endDate)
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 代表自然日(一天就代表一个自然日)
int natureDay = 0;
// 代表休息日,假定周六周日休息
int restDay = 0;
// 如果开始时间为空,则为当前日期
Calendar now = Calendar.getInstance();
if (beginDate != null) {
try {
now.setTime(sdf.parse(beginDate));
} catch (ParseException e) {
e.printStackTrace();
throw new ParseException("\n"
+ "DateUtils.work_rest_all() was error", 1);
}
}

Calendar end = Calendar.getInstance();
try {
end.setTime(sdf.parse(endDate));
} catch (ParseException e) {
e.printStackTrace();
throw new ParseException("\n"
+ "DateUtils.work_rest_all() was error", 2);
}

String nowStr = sdf.format(now.getTime());
String endStr = sdf.format(end.getTime());
if (now.before(end)) {
while (!nowStr.equals(endStr)) {
if (now.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| now.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
restDay++;
}
natureDay++;
now.add(Calendar.DAY_OF_YEAR, 1);
}
}
int[] result = { natureDay - restDay, restDay, natureDay };
return result;
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值