import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /*** * 日期時間相關的工具類 * @author huzf * */ public class DatetimeUtil { private static final Log log = LogFactory.getLog(DatetimeUtil.class); private static Map<String, DateFormat> formatMap = new HashMap<>(); /*** * 獲取當前時間的毫秒數,是自1970-1-01 00:00:00.000 到當前時刻的時間距離 * 如:1445090702909 * 多線程,有很多重復值 * @return 返回類型為String */ public static String getCurrentTimeMillis(){ return String.valueOf(System.currentTimeMillis()); } /*** * 獲取當前時間的納秒,1ms=1000000ns * 精准1000000倍的取納秒的方法 * 多線程,基本無重復值 * @return */ public static String getNanoTime(){ return String.valueOf(System.nanoTime()); } /*** * 返回當前年,如:2015 * @return */ public static String getCurrentYear(){ Calendar calendar=Calendar.getInstance(); return String.valueOf(calendar.get(Calendar.YEAR)); } /*** * 返回當前月,如:07 * @return */ public static String getCurrentMonth(){ Calendar calendar=Calendar.getInstance(); String month=String.valueOf(calendar.get(Calendar.MONTH)+1); if(month.length()==1){ return "0"+month; } return month; } /*** * 返回當前日,如:26 * @return */ public static String getCurrentDayOfMonth(){ Calendar calendar=Calendar.getInstance(); return String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)); } private static DateFormat getDateFormat(String pattern){ DateFormat format = formatMap.get(pattern); if(format == null){ format = new SimpleDateFormat(pattern); // format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); formatMap.put(pattern, format); } return format; } /** * 把時間字符串轉換成指定格式的時間 * @param pattern * @param dateString * @return Date */ public static Date parse(String pattern, String dateString){ try{ DateFormat format = getDateFormat(pattern); return format.parse(dateString); }catch (ParseException e){ log.error("時間解析錯誤 ", e); return null; } } /** * 把時間轉換成指定格式的時間字符串 * @param pattern * @param date * @return String */ public static String format(String pattern, Date date){ DateFormat format = getDateFormat(pattern); return format.format(date); } public static Date stringConvertDate(String dateString){ return parse("yyyy-MM-dd HH:mm:ss", dateString); // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // try { // return sdf.parse(date); // } catch (ParseException e) { // e.printStackTrace(); // return null; // } } public static Date stringConvertDate(String dateString,String pattern){ return parse(pattern, dateString); // SimpleDateFormat sdf = new SimpleDateFormat(p); // try { // return sdf.parse(date); // } catch (ParseException e) { // e.printStackTrace(); // return null; // } } public static Date convertDate(Date date,String p){ SimpleDateFormat sdf = new SimpleDateFormat(p); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { return sdf2.parse(sdf.format(date)); } catch (ParseException e) { log.error("DatetimeUtil.convertDate:"+e.getMessage()); return null; } } // public static void main(String[] args) { // Date d = DatetimeUtil.stringConvertDate("1991-07-30 00:00:00", "yyyy-MM-dd HH:mm:ss"); // System.out.println(d); // } /*** * 返回當前時間,如:2015-01 * @return */ public static String getDateTime(){ Calendar calendar=Calendar.getInstance(); String year= String.valueOf(calendar.get(Calendar.YEAR)); String month=String.valueOf(calendar.get(Calendar.MONTH)+1); if(month.length()==1){ month="0"+month; } String datetime=year+"-"+month; return datetime; } /*** * 返回當前時間的前一月份,如:2015-01前一月份是2014-12 * @return */ public static String getMonthAgo(){ Calendar calendar=Calendar.getInstance(); String year= String.valueOf(calendar.get(Calendar.YEAR)); String month=String.valueOf(calendar.get(Calendar.MONTH)+1); if(month.equals("1")){ year= String.valueOf(calendar.get(Calendar.YEAR)-1); month="12"; }else{ month=String.valueOf(calendar.get(Calendar.MONTH)); } if(month.length()==1){ month="0"+month; } String datetime=year+"-"+month; return datetime; } /*** * 返回當前時間的前年,如:2015前一年是2014 * @return */ public static String getYearAgo(){ Calendar calendar=Calendar.getInstance(); String year= String.valueOf(calendar.get(Calendar.YEAR)-1); return year; } /*** * 返回查詢時間 * @return */ static Calendar c = Calendar.getInstance(); // 獲取當前月 final int month = c.get(Calendar.MONTH) + 1; public int getmonth() { return month; } // 獲取到當前年 final int year = c.get(Calendar.YEAR); public int getYear() { return year; } // 獲取上一個季度 public int getCurrentQuarger() { if (month >= 1 && month <= 3) return 4; if (month >= 4 && month <= 6) return 1; if (month >= 7 && month <= 9) return 2; return 3; } // 獲取上一個月 public int getCurrentMonthAgo() { if (month == 1) { return 12; } int currentMonth = month - 1; return currentMonth; } // 獲取上一年 public int getCurrentYearAgo() { return year - 1; } // 判斷當前月分是否用生成季度報表 public boolean isCreateQuarter() { if (month == 1 || month == 4 || month == 7 || month == 10) { return true; } return false; } // 獲取季報表查詢開始時間 public String getQuarterBegin() { String date = null; String monthTemps = String.valueOf(month); int yearTemp = year; if (month == 1) { monthTemps = "10"; yearTemp = year - 1; } else { monthTemps = "0" + (month - 3); } if (this.isCreateQuarter()) { date = yearTemp + "-" + monthTemps + "-" + "01" + " 00:00:00"; } return date; } // 獲取到即報表上上季度開始時間 public String getQuarerLastBengin() { String date = null; String monthTemps = String.valueOf(month); int yearTemp = year; if (month == 1) { monthTemps = "07"; yearTemp = year - 1; } else if (month == 4) { monthTemps = "10"; yearTemp = year - 1; } else { monthTemps = "0" + (month - 6); } if (this.isCreateQuarter()) { date = yearTemp + "-" + monthTemps + "-" + "01" + " 00:00:00"; } return date; } // 獲取季報表查詢結束時間 public String getQuarterEnd() { String date = null; String monthTemps = String.valueOf(month); if (month < 10) monthTemps = "0" + monthTemps; if (this.isCreateQuarter()) { date = year + "-" + monthTemps + "-" + "01" + " 00:00:00"; } return date; } // 獲取到即報表上上季度結束時間 public String getQuarerLastEnd() { String date = null; String monthTemps = String.valueOf(month); int yearTemp = year; if (month == 1) { monthTemps = "10"; yearTemp = year - 1; } else if (month == 4) { monthTemps = "01"; } else { monthTemps = "0" + (month - 3); } if (this.isCreateQuarter()) { date = yearTemp + "-" + monthTemps + "-" + "01" + " 00:00:00"; } return date; } // 獲取月報表查詢開始時間 public String getMonthBegin() { String date = null; String monthTemps = String.valueOf(month); int yearTemp = year; if (month == 1) { monthTemps = "12"; yearTemp = year - 1; } else if (month < 11) { monthTemps = "0" + (month - 1); } date = yearTemp + "-" + monthTemps + "-" + "01" + " 00:00:00"; return date; } // 獲取上上月報表查詢開始時間 public String getMonthLastBegin() { String date = null; String monthTemps = String.valueOf(month); int yearTemp = year; if (month == 1) { monthTemps = "11"; yearTemp = year - 1; } else if (month == 2) { monthTemps = "12"; yearTemp = year - 1; } else if (month < 12) { monthTemps = "0" + (month - 2); } else { monthTemps = String.valueOf(month - 1); } date = yearTemp + "-" + monthTemps + "-" + "01" + " 00:00:00"; return date; } // 獲取月報表查詢結束時間 public String getMonthEnd() { String date = null; String monthTemps = String.valueOf(month); if (month < 10) { monthTemps = "0" + month; } date = year + "-" + monthTemps + "-" + "01" + " 00:00:00"; return date; } // 獲取上上月報表查詢結束時間 public String getMonthLastEnd() { String date = null; String monthTemps = String.valueOf(month); int yearTemp = year; if (month == 1) { monthTemps = "12"; yearTemp = year - 1; } if (month < 11) { monthTemps = "0" + (month - 1); } else { monthTemps = String.valueOf(month - 1); } date = yearTemp + "-" + monthTemps + "-" + "01" + " 00:00:00"; return date; } //獲取上個月的最后一天 public static String getMonthLastDay(){ Calendar cal = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.DATE, -1); String data =df.format(cal.getTime()); return data; } //獲取本月的最后一天 public String getThisMonthLastDay(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar ca = Calendar.getInstance(); ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); String last = format.format(ca.getTime()); return last; } //獲取某月有幾天 public static Integer getThisMonthDay(String pTime){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); try { c.setTime(format.parse(pTime)); } catch (ParseException e) { log.error("DatetimeUtil.getThisMonthDay:"+e.getMessage()); } System.out.println("------------" + c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH) + 1) + "月的天數和周數-------------"); return c.getActualMaximum(Calendar.DAY_OF_MONTH); } //獲取上個月的第一天 public static String getMonthFirstDay(){ Calendar cal = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); cal.add(Calendar.MONTH, -1); cal.set(Calendar.DAY_OF_MONTH, 1); String data = df.format(cal.getTime()); return data; } //獲取本月的第一天 public String getThisMonthFirstDay(){ Calendar cal = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); cal.add(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); String data = df.format(cal.getTime()); return data; } // 獲取月報表名字前綴 public String getMonthName() { int yearTemp = year; if (month == 1) { yearTemp = this.getCurrentYearAgo(); } return yearTemp + "-" + this.getCurrentMonthAgo(); } // 獲取季報表名字前綴 public String getQuarterName() { int yearTemp = year; if (month == 1) { yearTemp = this.getCurrentYearAgo(); } return yearTemp + "第" + this.getCurrentQuarger() + "季度"; } //獲取當前日期是周幾 public static int getDay(){ int getWeek = c.get(Calendar.DAY_OF_WEEK); if (getWeek == 1) { return 7; }else { return getWeek - 1; } } // 獲取到本周周一//此數據不穩定請勿用 public String getMonday() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 獲取本周一的日期 cal.add(Calendar.WEEK_OF_YEAR, 0); String data =df.format(cal.getTime()); return data; } // 獲取到本周周一//獲取本周周一請用此方法 public String getMondaytoday() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(new Date()); int day_of_week = c.get(Calendar. DAY_OF_WEEK) - 1; if (day_of_week == 0 ) { day_of_week = 7 ; } c.add(Calendar.DATE , -day_of_week + 1 ); c.add(Calendar.WEEK_OF_YEAR, 0); String data =df.format(c.getTime()); return data; } //獲取本周周日 public String getMondaySumDay() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.setTime(new Date()); int day_of_week = c.get(Calendar. DAY_OF_WEEK) - 1; if (day_of_week == 0 ) { day_of_week = 7 ; } c.add(Calendar.DATE , -day_of_week + 1 ); c.add(Calendar.WEEK_OF_YEAR, 0); c.add(Calendar.DAY_OF_YEAR,6); String data =df.format(c.getTime()); System.out.print(data); return data; } // 獲取到上個周周一 public String getMondayLast() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 獲取本周一的日期 cal.add(Calendar.WEEK_OF_YEAR, -1); String data =df.format(cal.getTime()); return data; } //獲取三個周前的周一 public String getMondayThree() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 獲取本周一的日期 cal.add(Calendar.WEEK_OF_YEAR, -3); String data =df.format(cal.getTime()); return data; } // 獲取到上個周周天 public String getLastSum() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 獲取本周一的日期 cal.add(Calendar.WEEK_OF_YEAR, -1); cal.add(Calendar.DATE, +6); String data =df.format(cal.getTime()); return data; } //獲取周報名字 public String getMondayName(){ return getMondayLast()+"到"+getMonday(); } //獲取兩分鍾前 public String getLastMinute(){ SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, -15);//15分鍾前 String data=time.format(cal.getTime()); return data; } //獲取當前時間 public String getMinute(){ SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); String data=time.format(cal.getTime()); return data; } public String getMinutes(){ SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); String data=time.format(cal.getTime()); return data; } //獲取上一天 public static String getLastDay(){ SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -1);//獲取一天前 String data=time.format(cal.getTime()); return data; } //獲取七天前日期 public static String getLastweek(){ SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, -7);//7前 String data=time.format(cal.getTime()); return data; } // 獲取到七個周前周一 public String getMondayLastSeven() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // c.add(Calendar.WEEK_OF_YEAR, -7); String data =df.format(c.getTime()); return data; } // 獲取到半年前時間 public String getSexMonth() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR,Calendar.MONDAY-6,Calendar.DATE); String data =df.format(cal.getTime()); return data; } /** *@Title getViewTime * @author lize16713 * @Description 獲取年數 */ public static String getViewTime(Long ms){ double yesr = ms/(1000.0*60.0*60.0*24.0*30.0*12.0); String time = String.format("%.1f", yesr); if(time.contains("-")){ return time = "0 年"; } String[] str = time.split("\\."); int i= Integer.parseInt(str[0]); int d = Integer.parseInt(str[1]); if(i>0&&d>=5){ time = str[0]+".5 年"; }else if(i>0&&d<5){ time = str[0]+" 年"; }else if(i<=0&&d>=5){ time = "1 年"; }else{ time = "0.5 年"; } return time; } /** * yangkai17025 * 給定時間判斷此時間所在周的周一*(周日) * **/ public String convertWeekByDate(Date time) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //設置時間格式 Calendar cal = Calendar.getInstance(); cal.setTime(time); //判斷要計算的日期是否是周日,如果是則減一天計算周六的,否則會出問題,計算到下一周去了 int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } System.out.println("要計算日期為:"+sdf.format(cal.getTime())); //輸出要計算日期 cal.setFirstDayOfWeek(Calendar.MONDAY);//設置一個星期的第一天,按中國的習慣一個星期的第一天是星期一 int day = cal.get(Calendar.DAY_OF_WEEK);//獲得當前日期是一個星期的第幾天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根據日歷的規則,給當前日期減去星期幾與一個星期第一天的差值 String imptimeBegin = sdf.format(cal.getTime()); // 周一 // cal.add(Calendar.DATE, 6); // String imptimeEnd = sdf.format(cal.getTime());//周天 return imptimeBegin; } /** * yangkai17025 * * **/ public static class MyDate{ int day; int month; int year; SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy"); SimpleDateFormat sdf1 = new SimpleDateFormat("MM"); SimpleDateFormat sdf2= new SimpleDateFormat("dd"); public MyDate(Date d){ this.day = Integer.valueOf(sdf2.format(d)); this.month = Integer.valueOf(sdf1.format(d)); this.year = Integer.valueOf(sdf0.format(d));; } public long funtion(MyDate d){ int newDay = d.day; int newMonth = d.month; int newYear = d.year; Calendar c1 = Calendar.getInstance(); c1.set(newYear,newMonth,newDay); long n1 = c1.getTimeInMillis(); Calendar c2 = Calendar.getInstance(); c2.set(year,month,day); long n2 = c2.getTimeInMillis(); return Math.abs((n1 - n2)/24/3600000); } } /** * 兩個時間之間的查(天) * yangkai17025 * * **/ public static long getDateDiffe(Date a,Date b){ MyDate d1 = new MyDate(a); MyDate d2 = new MyDate(b); return d1.funtion(d2); } /** * 獲取本月有多少天) * yangkai17025 * * **/ public static int getDateOfMonth(){ Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE)); return cal.get(Calendar.DAY_OF_MONTH); } /** *返回給定時間是周幾 * @throws ParseException **/ public static int getDayForWeek(String pTime){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); try { c.setTime(format.parse(pTime)); } catch (ParseException e) { log.error("DatetimeUtil.getDayForWeek:"+e.getMessage()); } int dayForWeek = 0; if(c.get(Calendar.DAY_OF_WEEK) == 1){ dayForWeek = 7; }else{ dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; } return dayForWeek; } /** * 更新work_day數據 */ public static void updateWorkDay(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); int year= 2017; int m=1;//月份計數 while (m<13){ int month=m; Calendar cal=Calendar.getInstance();//獲得當前日期對象 cal.clear();//清除信息 cal.set(Calendar.YEAR,year); cal.set(Calendar.MONTH,month-1);//1月從0開始 cal.set(Calendar.DAY_OF_MONTH,1);//設置為1號,當前日期既為本月第一天 int count=cal.getActualMaximum(Calendar.DAY_OF_MONTH); System.out.println("當月天數" + count); System.out.println("INSERT INTO sys_workday (create_time,update_time,work_day) VALUES (NOW(),NOW(),'"+sdf.format(cal.getTime())+"');");//當月第一天 for (int j=0;j<=(count - 2);){ cal.add(Calendar.DAY_OF_MONTH,+1); j++; System.out.println("INSERT INTO sys_workday (create_time,update_time,work_day) VALUES (NOW(),NOW(),'"+sdf.format(cal.getTime())+"');"); } m++; } } /** * 獲取給定時間的星期 * @param time * @return */ public static String getWeek(Date time){ if(time == null){ return null; } Calendar c = Calendar.getInstance(); c.setTime(time); int week = c.get(Calendar.DAY_OF_WEEK) - 1; if (week == 1) { return "星期一"; } if (week == 2) { return "星期二"; } if (week == 3) { return "星期三"; } if (week == 4) { return "星期四"; } if (week == 5) { return "星期五"; } if (week == 6) { return "星期六"; } if (week == 0) { return "星期日"; } return null; } /** * 獲取當前時間前num天的日期 * * @param num * @return */ public static List<String> getDateList(int num) { List<String> dateList = new ArrayList<>(); Calendar c = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); dateList.add(sdf.format(c.getTime())); if (num <= 0) { return dateList; } for (int i = 1; i < num; i++) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, -i); dateList.add(sdf.format(calendar.getTime())); calendar.clear(); } return dateList; } public static void main(String[] args) { updateWorkDay(); } /** *給定String返回data * @throws ParseException **/ public static Date getDayOfStirng(String pTime) throws ParseException{ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.parse(pTime); } }