package com.rc.openapi.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("unchecked")
public class DateUtil {
private static SimpleDateFormat dateformater;
static java.text.SimpleDateFormat sdfShort = new java.text.SimpleDateFormat("yyyyMMdd");
public static java.text.SimpleDateFormat sdfLong = new java.text.SimpleDateFormat("yyyy-MM-dd");
static java.text.SimpleDateFormat sdfLongTime = new java.text.SimpleDateFormat("yyyyMMddHHmmss");
static java.text.SimpleDateFormat sdfLongTimePlus = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static java.text.SimpleDateFormat sdfLongTimePlusMill = new java.text.SimpleDateFormat("yyyyMMddHHmmssSSSS");
private static long DAY_IN_MILLISECOND = 0x5265c00L;
/**
* @param date
* @param format 日期格式
* @return String
* @author zhangyong
*/
public static String DateToStr(Date date, String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
} catch (Exception e) {
System.out.println("Date 转 String 类型失败: " + e);
return null;
}
}
/**
* 把字符型日期表达式转换成java.util.Date
*
* @param strDate 型如 "yyyy-M-dd" 的日期字符串
* @return 转换后的java.util.Date对象;转换失败返回null
*/
public Date parseDate(String strDate) {
Date date = null;
try {
date = getDateFormater().parse(strDate);
} catch (Exception ex) {
// System.err.println(ex.getMessage());
}
return date;
}
private DateFormat getDateFormater() {
if (dateformater == null)
dateformater = new SimpleDateFormat("yyyy-M-dd");
return dateformater;
}
/**
* @param Date date, int days
* @return DATE 型加具体的天数
* @author zhangyong
*/
public static Date dateAddDays(Date date, int days) {
long now = date.getTime() + (long) days * DAY_IN_MILLISECOND;
return new Date(now);
}
public static String getDate() {
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMdd");
return df.format(date);
}
/**
* @param Date date,String fFormatStr eg:yyyy-MM-dd HH:mm:ss
* @return 将DATE 转换成字符性日期格式
* @author zhangyong
*/
public static String dateTypeToString(Date date, String fFormatStr) {
//yyyy-MM-dd HH:mm:ss
SimpleDateFormat dateformat = new SimpleDateFormat(fFormatStr);
String strDate = dateformat.format(date);
return strDate;
}
/**
* @param yyyy-MM-dd
* @author zhangyong
* @获取当前的系统时间,并按照固定的格式初始话
*/
public static String getStringOfNowDate(String fFormatStr) {
String nowDateString = dateTypeToString(new Date(), fFormatStr);
return nowDateString;
}
/**
* @param yyyy-MM-dd
* @author zhangyong
* @获取当前的系统时间,并按照固定的格式初始话
*/
public static Date getDateOfNowDate(String fFormatStr) {
String nowDateString = dateTypeToString(new Date(), fFormatStr);
return new DateUtil().parseDate(nowDateString);
}
/**
* @ author zhangyong
* @ 获取当月的第一天,2009-05-01
*/
public static String getStringOfFirstDayInMonth() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String temp = sdf.format(date);
String firstDayInMoth = "";
firstDayInMoth = temp + "-01";
return firstDayInMoth;
}
/**
* @ author zhangyong
* @ 获取当月的第一天,2009-05-01
*/
public static Date getDateOfFirstDayInMonth() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
String temp = sdf.format(date);
String firstDayInMoth = "";
firstDayInMoth = temp + "-01";
return new DateUtil().parseDate(firstDayInMoth);
}
/**
* Descrption:取得当前日期,格式为:yyyy-MM-dd HH:mm:ss
*
* @return String
* @throws java.lang.Exception
*/
public static String getPlusTime2(Date date) {
if (date == null) return null;
try {
String nowDate = sdfLongTimePlus.format(date);
return nowDate;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* @param String strDate,String fFormatStr eg :yyyy-MM-dd HH:mm:ss
* @return Date 字符串形式日期装换成date类型
* @author zhangyong
*/
public static Date strDateConvertToDate(String strDate, String fFormatStr) {
java.util.Date birthday = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(fFormatStr);
try {
birthday = sdf.parse(strDate);
return birthday;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* Date字符串形式日期装换成date类型,不进行日志的自动加算
*
* @param strDate
* @param fFormatStr eg:yyyy-MM-dd HH:mm:ss
* @author zhang_gl
*/
public static Date strToDateNoCalculate(String strDate, String fFormatStr) {
try {
java.util.Date birthday = new java.util.Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(fFormatStr);
sdf.setLenient(false);
birthday = sdf.parse(strDate);
return birthday;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 基准日期增加年数、月数、天数、小时、分钟、秒数,参数为0时,不修改对应项目
*
* @param date 基准日期
* @param date 年份增量
* @param date 月份增量
* @param date 日期增量
* @param date 时钟增量
* @param date 分钟增量
* @param date 秒钟增量
* @author zhang_gl
*/
public static Date dateAddByProject(Date date, int year, int month, int day, int hour, int minute, int second) {
try {
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (year != 0) {
cal.add(Calendar.YEAR, year);
}
if (month != 0) {
cal.add(Calendar.MONTH, month);
}
if (day != 0) {
cal.add(Calendar.DAY_OF_MONTH, day);
}
if (hour != 0) {
cal.add(Calendar.HOUR_OF_DAY, hour);
}
if (minute != 0) {
cal.add(Calendar.MINUTE, minute);
}
if (second != 0) {
cal.add(Calendar.SECOND, second);
}
return cal.getTime();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 得到当前日期时间,格式为yyyy-MM-dd hh:mm:ss.
*
* @return String
*/
public static String getCurrDateTime() {
java.sql.Timestamp date = new java.sql.Timestamp(System
.currentTimeMillis());
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return formatter.format(date);
}
public static String getDateStrNoSign(Date date) {
if (null != date) {
return sdfLongTime.format(date);
}
return null;
}
public static Date getDateFormString(String str) {
if (!"".equals(str)) {
try {
return sdfLongTimePlus.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 判断当前日期是否在指定日期范围内
*/
public static boolean isBelongCalendar(Date _time, String _from, String _to) {
if (_from == null || _from.equals("") || _to == null || _to.equals("")) {
return false;
}
Date from = stringToDate(_from);
Date to = stringToDate(_to);
Calendar date = Calendar.getInstance();
date.setTime(_time);
Calendar after = Calendar.getInstance();
after.setTime(from);
Calendar before = Calendar.getInstance();
before.setTime(to);
if (date.after(after) && date.before(before)) {
return true;
} else {
return false;
}
}
/**
* String类型转Date类型的时间
*/
public static Date stringToDate(String time) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = null;
try {
date = format.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static String objToStr(Object obj) {
if (null == obj) {
return "";
}
return obj.toString();
}
public static String removeComma(String str) {
if (null == str || "".equals(str)) {
return "";
}
if (str.indexOf(",") != -1) {
return str.replaceAll(",", "");
}
return str;
}
/**获取上n个小时整点小时时间
* @param date
* @return
*/
public static Date getLastHourTime(Date date,int n){
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY)-n);
date = ca.getTime();
return date;
}
/**获取当前时间的整点小时时间
* @param date
* @return
*/
public static Date getCurrHourTime(Date date){
Calendar ca = Calendar.getInstance();
ca.setTime(date);
ca.set(Calendar.MINUTE, 0);
ca.set(Calendar.SECOND, 0);
date = ca.getTime();
return date;
}
public static void main(String[] args) {
Date time = new Date();
System.out.println(isBelongCalendar(time, "2017-3-11 10:00:05", ""));
System.out.println(isBelongCalendar(time, null, ""));
}
}
04-04
515
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-01