private static SimpleDateFormat fullSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final String format ="yyyy-MM-dd HH:mm:ss";
public static final String formatDay ="yyyy-MM-dd";
public static final String formatForFile ="yyyyMMddHHmmss";
public static final String formatStr ="yyyy年MM月dd日";
private static Date rightNow = null;
public static Calendar calendar = null;
/**
* 将日期类型的字符串转成Date类型,格式:yyyy-MM-dd HH:mm:ss
* @param convertDate
* @return
*/
public static Date getTimeStringByFullString(String convertDate) {
Date timeString = null;
try {
timeString = fullSimpleDateFormat.parse(convertDate);
} catch (Exception ex) {
ex.printStackTrace();
}
return timeString;
}
/**
* 获取Date类型的当前系统日期:Tue Sep 05 20:01:06 CST 2017
* @return
*/
public static Date getSystemTimeByFullDate() {
Date timeString = null;
try {
rightNow = new Date();
String date= fullSimpleDateFormat.format(rightNow);
timeString = fullSimpleDateFormat.parse(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return timeString;
}
/**
* 功能描述:格式化输出日期
* @param date Date 日期
* @param format String 格式
* @return 返回字符型日期
*/
public static String format(Date date, String format) {
String result = "";
try {
if (date != null) {
DateFormat dateFormat = new SimpleDateFormat(format);
result = dateFormat.format(date);
}
} catch (Exception e) {
}
return result;
}
/**
* 将字符串转成Date类型
* @param source 字符串类型的日期
* @param pattern 日期格式,默认格式为yyyy-MM-dd
* @return
*/
public static Date convertStringToDate(String source, String pattern) {
if (pattern.equals("") || null == pattern) {
pattern = "yyyy-MM-dd";
}
Date date = null;
try {
date = DateUtils.parseDate(source, new String[] { pattern });
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 比较字符串类型的时间,获取相差时间
* @param time1 开始时间(小)
* @param time2 结束时间(大)
* @return
*/
public static String compareTime(String time1,String time2){
String retime = "";
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat smdf = new SimpleDateFormat(format);
try {
Date start = smdf.parse(time1);
Date end = smdf.parse(time2);
long t = (end.getTime() - start.getTime()) /1000;
if(t>=60&&t<3600){
System.out.println("相差:" + t/60 + "分钟");
Long m = t/60;
retime = "0";
if(m > 2){//关键业务逻辑:短信密码超过3分钟就失效
retime = "1";
}else{
retime = "2";
}
}else if(t>=3600&&t<(3600*24)){
System.out.println("相差:" + t/3600 + "小时");
retime = "3";
}else if(t>=(3600*24)){
System.out.println("相差:" + t/(3600*24) + "天");
retime = "4";
}else{
System.out.println("相差:" + t + "秒");
retime = "5";
}
} catch (Exception e) {
e.printStackTrace();
}
return retime;
}
/**
* 功能描述:日期相加
* @param date Date 日期
* @param day int 天数
* @return 返回相加后的日期
*/
public static Date addDate(Date date, int day) {
calendar = Calendar.getInstance();
long millis = getMillis(date) + ((long) day) * 24 * 3600 * 1000;
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
/**
*功能描述:时间比较大小
*@param time1
*@param time2
*@return
*/
public static boolean DateAfter(Timestamp time1, Timestamp time2) {
if (time1.after(time2))
return true;
else
return false;
}
/**
* 功能描述:返回毫秒
* @param date 日期
* @return 返回毫秒
*/
public static long getMillis(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
/**
*功能描述:把Timestamp时间类型转换成字符串类型
*@param convertDate
*@return
*/
public static String getTimestampString(Timestamp convertDate) {
String t1 = fullSimpleDateFormat.format(convertDate);
return t1;
}
/**
*功能描述:按照 'yyyy-MM-dd HH:mm:ss' 格式格式化时间
*@param date
*@return
*/
public static String getByFullString(Date date) {
return fullSimpleDateFormat.format(date);
}
/**
* 格式化日期(返回当前的日期,例如 "2016-05-28")
*/
public static String getThisDate(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前的年份,例如 "2016")
*/
public static String getThisYear(){
DateFormat format = new SimpleDateFormat("yyyy");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前的月份,例如 "01")
*/
public static String getThisMonth(){
DateFormat format = new SimpleDateFormat("MM");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前系统时间,格式“yyyyMMddHHmmss”)
*/
public static String getSystemDateByyyyyMMddHHmmss(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前系统时间,格式“yyyyMMdd”)
*/
public static String getSystemDateByyyyyMMdd(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前系统时间的前一天,格式“yyyyMMdd”)
*/
public static String getSystemDateLastDayByyyyyMMdd() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(date);
}
/**
* 格式化日期(返回当前系统时间的前一天,格式“yyyy-MM-dd”)
*/
public static String getSystemDateLastDayByyyyy_MM_dd() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
/**
* 格式化日期(返回当前系统时间的上一年,格式“yyyyMMdd”)
*/
public static String getSystemDateLastYearByyyyyMMdd() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.YEAR, -1);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(date);
}
/**
* 格式化日期(返回当前系统时间的后一分钟,格式“yyyy-MM-dd HH:mm:ss”)
*/
public static String getSystemDateNextMinuteByyyyyMMdd(Date datePara) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(datePara);
calendar.add(Calendar.MINUTE,1);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
/**
* 格式化日期(返回当前系统时间的前的第30天,格式“yyyy-MM-dd HH:mm:ss”)
*/
public static String getDateByyyyyMMddBefore30Day(Date datePara) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(datePara);
calendar.add(Calendar.DAY_OF_MONTH,-30);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
public static final String format ="yyyy-MM-dd HH:mm:ss";
public static final String formatDay ="yyyy-MM-dd";
public static final String formatForFile ="yyyyMMddHHmmss";
public static final String formatStr ="yyyy年MM月dd日";
private static Date rightNow = null;
public static Calendar calendar = null;
/**
* 将日期类型的字符串转成Date类型,格式:yyyy-MM-dd HH:mm:ss
* @param convertDate
* @return
*/
public static Date getTimeStringByFullString(String convertDate) {
Date timeString = null;
try {
timeString = fullSimpleDateFormat.parse(convertDate);
} catch (Exception ex) {
ex.printStackTrace();
}
return timeString;
}
/**
* 获取Date类型的当前系统日期:Tue Sep 05 20:01:06 CST 2017
* @return
*/
public static Date getSystemTimeByFullDate() {
Date timeString = null;
try {
rightNow = new Date();
String date= fullSimpleDateFormat.format(rightNow);
timeString = fullSimpleDateFormat.parse(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return timeString;
}
/**
* 功能描述:格式化输出日期
* @param date Date 日期
* @param format String 格式
* @return 返回字符型日期
*/
public static String format(Date date, String format) {
String result = "";
try {
if (date != null) {
DateFormat dateFormat = new SimpleDateFormat(format);
result = dateFormat.format(date);
}
} catch (Exception e) {
}
return result;
}
/**
* 将字符串转成Date类型
* @param source 字符串类型的日期
* @param pattern 日期格式,默认格式为yyyy-MM-dd
* @return
*/
public static Date convertStringToDate(String source, String pattern) {
if (pattern.equals("") || null == pattern) {
pattern = "yyyy-MM-dd";
}
Date date = null;
try {
date = DateUtils.parseDate(source, new String[] { pattern });
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 比较字符串类型的时间,获取相差时间
* @param time1 开始时间(小)
* @param time2 结束时间(大)
* @return
*/
public static String compareTime(String time1,String time2){
String retime = "";
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat smdf = new SimpleDateFormat(format);
try {
Date start = smdf.parse(time1);
Date end = smdf.parse(time2);
long t = (end.getTime() - start.getTime()) /1000;
if(t>=60&&t<3600){
System.out.println("相差:" + t/60 + "分钟");
Long m = t/60;
retime = "0";
if(m > 2){//关键业务逻辑:短信密码超过3分钟就失效
retime = "1";
}else{
retime = "2";
}
}else if(t>=3600&&t<(3600*24)){
System.out.println("相差:" + t/3600 + "小时");
retime = "3";
}else if(t>=(3600*24)){
System.out.println("相差:" + t/(3600*24) + "天");
retime = "4";
}else{
System.out.println("相差:" + t + "秒");
retime = "5";
}
} catch (Exception e) {
e.printStackTrace();
}
return retime;
}
/**
* 功能描述:日期相加
* @param date Date 日期
* @param day int 天数
* @return 返回相加后的日期
*/
public static Date addDate(Date date, int day) {
calendar = Calendar.getInstance();
long millis = getMillis(date) + ((long) day) * 24 * 3600 * 1000;
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
/**
*功能描述:时间比较大小
*@param time1
*@param time2
*@return
*/
public static boolean DateAfter(Timestamp time1, Timestamp time2) {
if (time1.after(time2))
return true;
else
return false;
}
/**
* 功能描述:返回毫秒
* @param date 日期
* @return 返回毫秒
*/
public static long getMillis(Date date) {
calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.getTimeInMillis();
}
/**
*功能描述:把Timestamp时间类型转换成字符串类型
*@param convertDate
*@return
*/
public static String getTimestampString(Timestamp convertDate) {
String t1 = fullSimpleDateFormat.format(convertDate);
return t1;
}
/**
*功能描述:按照 'yyyy-MM-dd HH:mm:ss' 格式格式化时间
*@param date
*@return
*/
public static String getByFullString(Date date) {
return fullSimpleDateFormat.format(date);
}
/**
* 格式化日期(返回当前的日期,例如 "2016-05-28")
*/
public static String getThisDate(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前的年份,例如 "2016")
*/
public static String getThisYear(){
DateFormat format = new SimpleDateFormat("yyyy");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前的月份,例如 "01")
*/
public static String getThisMonth(){
DateFormat format = new SimpleDateFormat("MM");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前系统时间,格式“yyyyMMddHHmmss”)
*/
public static String getSystemDateByyyyyMMddHHmmss(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前系统时间,格式“yyyyMMdd”)
*/
public static String getSystemDateByyyyyMMdd(){
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String newDate = format.format(new Date());
return newDate;
}
/**
* 格式化日期(返回当前系统时间的前一天,格式“yyyyMMdd”)
*/
public static String getSystemDateLastDayByyyyyMMdd() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(date);
}
/**
* 格式化日期(返回当前系统时间的前一天,格式“yyyy-MM-dd”)
*/
public static String getSystemDateLastDayByyyyy_MM_dd() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_MONTH, -1);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
}
/**
* 格式化日期(返回当前系统时间的上一年,格式“yyyyMMdd”)
*/
public static String getSystemDateLastYearByyyyyMMdd() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.YEAR, -1);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
return format.format(date);
}
/**
* 格式化日期(返回当前系统时间的后一分钟,格式“yyyy-MM-dd HH:mm:ss”)
*/
public static String getSystemDateNextMinuteByyyyyMMdd(Date datePara) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(datePara);
calendar.add(Calendar.MINUTE,1);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}
/**
* 格式化日期(返回当前系统时间的前的第30天,格式“yyyy-MM-dd HH:mm:ss”)
*/
public static String getDateByyyyyMMddBefore30Day(Date datePara) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(datePara);
calendar.add(Calendar.DAY_OF_MONTH,-30);
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
}