You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.5 KiB
Java

package com.manage.util;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
/**
* Format String : yyyy-MM-dd HH:mm:ss
*/
public static final String DateFormat1 = "yyyy-MM-dd HH:mm:ss";
/**
* Format String : yyyy-MM-dd
*/
public static final String DateFormat2 = "yyyy-MM-dd";
/**
* Format String : yyyyMMdd
*/
public static final String DateFormat3 = "yyyyMMdd";
/**
* Format String : yyyyMMdd HHmmss
*/
public static final String DateFormat4 = "yyyyMMdd HHmmss";
/**
* Format String : yyyy-MM-dd HH:mm
*/
public static final String DateFormat5 = "yyyy-MM-dd HH:mm";
/**
* Format String : yyyyMMdd HH:mm
*/
public static final String DateFormat6 = "yyyyMMdd HH:mm";
/**
* Format String : yyyyMMdd
*/
public static final String DateFormat7 = "yyyy年MM月dd日";
/**
*
*
* @return Date
*/
public static Date getDate() {
Calendar calendar = Calendar.getInstance();
return calendar.getTime();
}
/**
*
*
* @param format
* @return string
*/
public static String getDate(String format) {
return getStringDate(getDate(), format);
}
/**
*
*
* @param date Date
* @param method
* @return
*/
public static String getStringDate(Date date, String method) {
SimpleDateFormat sdf = new SimpleDateFormat(method);
String ret = null;
try {
ret = sdf.format(date);
} catch (Exception ex) {
ex.printStackTrace();
}
return ret;
}
/**
*
*
* @param date Date
* @param days
* @return Date
*/
public static Date getDate(Date date, int days) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, days);
return calendar.getTime();
}
/**
*
*
* @param dateStr 'yyyyMMdd'
* @param days
* @return Date
*/
public static Date getDate(String dateStr, int days) {
return getDate(getDate(dateStr, DateFormat3), days);
}
/**
* StringDate
*
* @param stringDate
* @param method
* @return Date
*/
public static Date getDate(String stringDate, String method) {
SimpleDateFormat sdf = new SimpleDateFormat(method);
Date ret = null;
try {
String integerDate = stringDate.replaceAll("/", "").replaceAll("年", "").replaceAll("月", "").replaceAll("日", "").replaceAll("", ":");
ret = sdf.parse(integerDate);
} catch (Exception ex) {
ex.printStackTrace();
}
return ret;
}
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
*
*
* @param beginDate
* @param endDate
* @return
*/
public static int getDayCount(Date beginDate, Date endDate) {
int count = 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(beginDate);
while (calendar.getTime().before(endDate)) {
count++;
calendar.add(Calendar.DAY_OF_YEAR, 1);
}
return count;
}
/**
*
*
* @param data
* @return
*/
public static String getLastMonth(Date data) {
Calendar calendar = Calendar.getInstance();
Date date = new Date(System.currentTimeMillis());
calendar.setTime(date);
calendar.add(Calendar.MONTH, -1);
date = calendar.getTime();
return DateUtils.getStringDate(date, DateUtils.DateFormat2);
}
/**
*
*
* @param data
* @return
*/
public static String getNextMonth(Date data) {
Calendar calendar = Calendar.getInstance();
Date date = new Date(System.currentTimeMillis());
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
date = calendar.getTime();
return DateUtils.getStringDate(date, DateUtils.DateFormat2);
}
/**
* LONG
* @param diff
* @return
*/
public static String LongToString(long diff)
{
String showtime = "";
long oneSecond = 1000;
long oneMinute = oneSecond * 60;
long oneHour = oneMinute * 60;
long hours = diff / oneHour;
diff -= hours * oneHour;
long minutes = diff / oneMinute;
diff -= minutes * oneMinute;
long seconds = diff / oneSecond;
if (hours > 0){ showtime += hours + "时";}
if (minutes > 0){ showtime += minutes + "分";}
if (seconds > 0){ showtime += seconds + "秒";}
return showtime;
}
/*
* beginend
*
**/
public static Boolean IsDateExist(String startTime,String endTime){
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
Date now = null;
Date begin = null;
Date end = null;
try {
now = df.parse(df.format(new Date()));
begin = df.parse(startTime);
end = df.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar nowTime = Calendar.getInstance();
nowTime.setTime(now);
Calendar beginTime = Calendar.getInstance();
beginTime.setTime(begin);
Calendar endTime1 = Calendar.getInstance();
endTime1.setTime(end);
if (nowTime.before(endTime1) && nowTime.after(beginTime)) {
return true;
} else {
return false;
}
}
/*
* h
*
**/
public static Double getHourDiff(Date startTime,Date endTime){
long ms = endTime.getTime() - startTime.getTime();
if (ms < 0){ return 0d;}
return Math.floor(ms/1000/60/60);
}
}