| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- package platform.common.util;
- import java.sql.Timestamp;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.TimeZone;
- import org.apache.commons.lang3.StringUtils;
- /**
- * Created by luohaifeng on 2017/8/9.
- */
- public class DateUtil {
- public static String YYYY_MM_DD = "yyyy-MM-dd";
- public static String YYYY_MM = "yyyy-MM";
- public static String getCurrentDateString(String fmt) {
- SimpleDateFormat sdf = new SimpleDateFormat(fmt);
- return sdf.format(new Date());
- }
- public static String getCurrentDateString() {
- return getCurrentDateString(YYYY_MM_DD);
- }
- /**
- * 日期转字符串
- *
- * @param date
- * @return
- */
- public static String getTimeString(Date date) {
- if (null == date) {
- return "";
- } else {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- return simpleDateFormat.format(date);
- }
- }
- // 获取当前时间
- public static Date getNowDate() {
- TimeZone time = TimeZone.getTimeZone("Asia/Shanghai");
- TimeZone.setDefault(time);
- Date now = new Date();
- return now;
- }
- public static String getTime(Date date) {
- if (null == date) {
- return "";
- } else {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD);
- return simpleDateFormat.format(date);
- }
- }
- /**
- * 日期转字符串 yyyy-MM-dd
- *
- * @param date
- * @return
- */
- public static String getTimeStringShort(Date date) {
- if (null == date) {
- return "";
- } else {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- return simpleDateFormat.format(date);
- }
- }
-
- /**
- * 字符串转时间转换日期格式
- *
- * @param date 时间字符串
- * @param pattern 字符串时间格式
- * @return
- * @throws ParseException
- */
- public static Date parseTimeStringToDate(String date, String pattern) throws ParseException {
- if (null == date) {
- return null;
- } else {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
- return simpleDateFormat.parse(date);
- }
- }
- public static long getDay(String pay_time,Integer cycle) throws ParseException {
- Date date = DateUtil.parseTimeStringToDate(pay_time, DateUtil.YYYY_MM_DD);
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.MONTH, -cycle);
- return ( date.getTime()-calendar.getTimeInMillis()) / (24 * 60 * 60 * 1000);
- }
- public static Boolean isGenerate(String pay_time,Integer remind) throws ParseException {
- if(null == remind) {
- remind = 5;
- }
- Date date = DateUtil.parseTimeStringToDate(pay_time, DateUtil.YYYY_MM_DD);
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.DATE, -remind);
- if(calendar.getTimeInMillis()<=new Date().getTime()){
- return true;
- }
- return false;
- }
- /**
- * 2017-08-19 20:30
- *
- * @return
- */
- public static String[] getDate(String time) {
- String[] strs = time.split(":");
- return strs;
- }
- public static String getPayTime(Integer payment_cycle, String contract_start_date) throws ParseException {
- Calendar plan = Calendar.getInstance();
- plan.setTime(parseTimeStringToDate(contract_start_date, YYYY_MM_DD));
- plan.add(Calendar.MONTH, payment_cycle);
- return getTimeStringShort(plan.getTime());
- }
- public static String AddMonth(Integer month, String date) throws ParseException {
- Calendar plan = Calendar.getInstance();
- plan.setTime(parseTimeStringToDate(date, YYYY_MM_DD));
- plan.add(Calendar.MONTH, month);
- return getTimeStringShort(plan.getTime());
- }
- public static String getCurrentMonth( ) throws ParseException {
- Calendar plan = Calendar.getInstance();
- plan.setTime(new Date());
- return String.valueOf(plan.get(Calendar.MONTH)+1);
- }
- public static String getCurrentYear( ) throws ParseException {
- Calendar plan = Calendar.getInstance();
- plan.setTime(new Date());
- return String.valueOf(plan.get(Calendar.YEAR));
- }
- public static String getMonthStart( String date) throws ParseException {
- Calendar plan = Calendar.getInstance();
- plan.setTime(parseTimeStringToDate(date, YYYY_MM));
- plan.add(Calendar.MONTH, 0);
- plan.set(Calendar.DAY_OF_MONTH, 1);
- return getTime(plan.getTime());
- }
- public static String getMonthEnd( String date) throws ParseException {
- Calendar plan = Calendar.getInstance();
- plan.setTime(parseTimeStringToDate(date, YYYY_MM));
- plan.add(Calendar.MONTH, 1);
- plan.set(Calendar.DAY_OF_MONTH, 0);
- return getTime(plan.getTime());
- }
- public static Boolean compareTo(String time1,String time2) throws ParseException {
- Date date1 = DateUtil.parseTimeStringToDate(time1, YYYY_MM_DD);
- Date date2 = DateUtil.parseTimeStringToDate(time2, YYYY_MM_DD);
- if (date1.getTime() - date2.getTime() >= 0) {
- return true;
- } else {
- return false;
- }
- }
- public static void main(String args[]){
- try {
- System.out.println(getCurrentMonth());
- System.out.println(getCurrentYear());
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 两个日期之间的天数
- * @param date1
- * @param date2
- * @return
- * @throws ParseException
- */
- public static int getDateSpace(String date1, String date2)
- throws ParseException {
- Calendar calst = Calendar.getInstance();;
- Calendar caled = Calendar.getInstance();
- calst.setTime(DateUtil.parseTimeStringToDate(date1, YYYY_MM_DD));
- caled.setTime(DateUtil.parseTimeStringToDate(date2, YYYY_MM_DD));
-
- //设置时间为0时
- calst.set(Calendar.HOUR_OF_DAY, 0);
- calst.set(Calendar.MINUTE, 0);
- calst.set(Calendar.SECOND, 0);
- caled.set(Calendar.HOUR_OF_DAY, 0);
- caled.set(Calendar.MINUTE, 0);
- caled.set(Calendar.SECOND, 0);
- //得到两个日期相差的天数
- int days = ((int)(caled.getTime().getTime()/1000)-(int)(calst.getTime().getTime()/1000))/3600/24;
-
- return days;
- }
- //获取审核剩余时间 审核时间5天
- public static String getOverTimeStr(String create_time,Integer overDay) throws ParseException {
- String overTimeStr = "";
- if(null == overDay) {
- overDay = 5;
- }
- if(StringUtils.isNotBlank(create_time)) {
- Calendar overTime = Calendar.getInstance();
- Calendar now = Calendar.getInstance();
- overTime.setTime(DateUtil.parseTimeStringToDate(create_time, "yyyy-MM-dd HH:mm:ss"));
- overTime.add(Calendar.DATE, overDay);
- now.setTime(new Date());
-
- overTime.set(Calendar.MINUTE, 0);
- overTime.set(Calendar.SECOND, 0);
- now.set(Calendar.MINUTE, 0);
- now.set(Calendar.SECOND, 0);
-
- //得到两个日期相差的小时数
- int hours = ((int)(overTime.getTime().getTime()/1000)-(int)(now.getTime().getTime()/1000))/3600;
- if( hours/24 > 0) {
- overTimeStr = "剩余"+String.valueOf((int)Math.floor(hours/24))+"个工作日" ;//+(hours%24)+"小时";
- }else {
- overTimeStr = "超出"+String.valueOf((-1)*(int)Math.floor(hours/24))+"个工作日" ;//+(hours%24)+"小时";
- }
- }
- return overTimeStr;
- }
-
- //获取审核处理时间 审核时间5天
- public static String getDealTimeStr(String create_time,String deal_time,Integer overDay) throws ParseException {
- String overTimeStr = "";
- if(null == overDay) {
- overDay = 5;
- }
- if(StringUtils.isNotBlank(create_time) && StringUtils.isNotBlank(deal_time)) {
- Calendar overTime = Calendar.getInstance();
- Calendar dealTime = Calendar.getInstance();
- overTime.setTime(DateUtil.parseTimeStringToDate(create_time, "yyyy-MM-dd HH:mm:ss"));
- overTime.add(Calendar.DATE, overDay);
- dealTime.setTime(DateUtil.parseTimeStringToDate(deal_time, "yyyy-MM-dd HH:mm:ss"));
-
- overTime.set(Calendar.MINUTE, 0);
- overTime.set(Calendar.SECOND, 0);
- dealTime.set(Calendar.MINUTE, 0);
- dealTime.set(Calendar.SECOND, 0);
-
- //得到两个日期相差的小时数
- int hours = ((int)(overTime.getTime().getTime()/1000)-(int)(dealTime.getTime().getTime()/1000))/3600;
- if( hours/24 > 0) {
- overTimeStr = "提前"+String.valueOf((int)Math.floor(hours/24))+"个工作日完成";//+(hours%24)+"小时完成";
- }else {
- overTimeStr = "超出"+String.valueOf((-1)*(int)Math.floor(hours/24))+"个工作日完成";//+(hours%24)+"小时完成";
- }
- }
-
- return overTimeStr;
- }
- //5秒内不能重复同一请求
- public static boolean isFrequent(String time) throws ParseException {
- Date preTime = DateUtil.parseTimeStringToDate(time,"yyyy-MM-dd HH:mm:ss");
- Date currTime = new Date();
- if(currTime.getTime()-preTime.getTime()<5000) {
- return true;
- }
- return false;
- }
- // 获得当天0点时间
- public static Date getTimesmorning() {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.HOUR_OF_DAY, 0);
- cal.set(Calendar.SECOND, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.MILLISECOND, 0);
- return cal.getTime();
- }
- // 获得当天24点时间
- public static Date getTimesnight() {
- Calendar cal = Calendar.getInstance();
- cal.set(Calendar.HOUR_OF_DAY, 24);
- cal.set(Calendar.SECOND, 0);
- cal.set(Calendar.MINUTE, 0);
- cal.set(Calendar.MILLISECOND, 0);
- return cal.getTime();
- }
- // 获得本周一0点时间
- public static Date getTimesWeekmorning() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
- return cal.getTime();
- }
- // 获得本周日24点时间
- public static Date getTimesWeeknight() {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getTimesWeekmorning());
- cal.add(Calendar.DAY_OF_WEEK, 7);
- return cal.getTime();
- }
- // 获得本月第一天0点时间
- public static Date getTimesMonthmorning() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
- return cal.getTime();
- }
- // 获得本月最后一天24点时间
- public static Date getTimesMonthnight() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
- cal.set(Calendar.HOUR_OF_DAY, 24);
- return cal.getTime();
- }
- /**
- * 获取两个日期相差的月数
- */
- public static int getMonthDiff(Date d1, Date d2) {
- Calendar c1 = Calendar.getInstance();
- Calendar c2 = Calendar.getInstance();
- c1.setTime(d1);
- c2.setTime(d2);
- int year1 = c1.get(Calendar.YEAR);
- int year2 = c2.get(Calendar.YEAR);
- int month1 = c1.get(Calendar.MONTH);
- int month2 = c2.get(Calendar.MONTH);
- int day1 = c1.get(Calendar.DAY_OF_MONTH);
- int day2 = c2.get(Calendar.DAY_OF_MONTH);
- // 获取年的差值
- int yearInterval = year1 - year2;
- // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
- if (month1 < month2 || month1 == month2 && day1 < day2) {
- yearInterval--;
- }
- // 获取月数差值
- int monthInterval = (month1 + 12) - month2;
- if (day1 < day2) {
- monthInterval--;
- }
- monthInterval %= 12;
- int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
- return monthsDiff;
- }
- }
|