package com.java.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.List; public class DateUtil { /** * �ぶ蛌遙 date蛌String * @param date * @param pattern 蛌遙跡宒 yyyy-MM-dd HH:mm:ss;yyyy-mm-dd;yy-mm-dd; pattern峈砑蛌遙腔跡宒濬倰 * @return */ public static String formatDate2Str(Date date, String pattern) { if (null == date) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(pattern); String s = sdf.format(date); return s; } /** * �ぶ蛌遙 string蛌date * @param str * @param pattern * @return */ public static Date formatStr2Date(String str, String pattern) { Date d = null; if (null != str && !str.trim().equals("")) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); try { d = sdf.parse(str); } catch (ParseException e) { throw new RuntimeException(e); } } return d; } /** * 汜傖String濬倰�ぶ yyyy-MM-dd HH:mm:ss * @return */ public static String dateString(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()); } /** * �ぶ眈樓 2015-05-12 +3 2015-05-15 * @param d * @param day * @return */ public static Date addDate(Date d,long day) { long time = d.getTime(); day = day*24*60*60*1000; time+=day; return new Date(time); } /** * 趼睫揹�ぶ蛌遙傖笢恅跡宒�ぶ * 怀� 2014-07-07 怀堤 媼�珨侐爛ほ堎ほ� */ public String dateToCnDate(String date) { String result = ""; String [] cnDate = new String [] {"�","珨","媼","�","侐","拻","鞠","ほ","匐","嬝"}; String ten = "坋"; String [] dateStr = date.split("-"); for(int i=0; i<dateStr.length; i++) { for(int j=0; j<dateStr[i].length(); j++) { String charStr = dateStr[i]; String str = String.valueOf(charStr.charAt(j)); if(charStr.length()==2) { if(charStr.equals("10")) { result += ten; break; } else { if(j==0) { if(charStr.charAt(j)=='1') result += ten; else if(charStr.charAt(j)=='0') result += ""; else result += cnDate[Integer.parseInt(str)] + ten; } if (j == 1) { if (charStr.charAt(j) == '0') result += ""; else result += cnDate[Integer.parseInt(str)]; } } } else { result += cnDate[Integer.parseInt(str)]; } } if (i == 0) { result += "爛"; continue; } if (i == 1) { result += "堎"; continue; } if (i == 2) { result += "�"; continue; } } return result; } /** * 鳳�議跺奀潔僇笢腔�ぶ蹈桶 * 怀��ぶ峈 yyyy-MM-dd 跡宒 */ public List<String> getDatesBetween(String beginDate, String endDate) { List<String> dates = new ArrayList<String>(); int num = diffDate(stringToLong(beginDate), stringToLong(endDate)); for(int i = 0; i < num; i++) { dates.add(diffDate(beginDate, i)); } return dates; } public long stringToLong(String someDate) { String format = "yyyy-MM-dd"; if (someDate.length() > 10) { format = "yyyy-MM-dd HH:mm:ss"; } SimpleDateFormat dd = new SimpleDateFormat(format); try { return dd.parse(someDate).getTime(); } catch (Exception e) { return new Date().getTime(); } } /** * 謗�ぶ眈熬 ,殿隙眈船毞杅 * * @param date * @return * @throws ParseException */ public int diffDate(Date beginDate, Date endDate) { return diffDate(beginDate.getTime(), endDate.getTime()); } public int diffDate(long beginDate, long endDate) { long diff = endDate - beginDate; long mint = (diff) / (1000); int hor = (int) mint / 3600; int day = (int) hor / 24; return (day + 1); } public String diffDate(String beginDate, int num) { Date d = new Date(); d = new Date(stringToLong(beginDate)); Calendar c = Calendar.getInstance(); c.setTime(d); c.add(Calendar.DAY_OF_YEAR, num); return c.get(Calendar.YEAR) + "-" + towStr(c.get(Calendar.MONTH) + 1) + "-"+ towStr(c.get(Calendar.DATE)); } private String towStr(int n) { if (n < 10) return "0" + n; else return "" + n; } /** * 鳳�絞堎笢腔菴珨毞 * 殿隙跡宒 yyyy-MM-dd */ public String getFirstDateOfCurrentMonth() { Calendar c = Calendar.getInstance(); String date = c.get(Calendar.YEAR) + "-" + towStr(1 + c.get(Calendar.MONTH)) + "-01"; return date; } /** * 鳳�珨堎笢腔郔綴珨毞 * 殿隙跡宒 yyyy-MM-dd */ public String getLastDateOfMonth() { Calendar c = new GregorianCalendar(); c.set(Calendar.DATE, 1); c.roll(Calendar.DATE, -1); String date = c.get(Calendar.YEAR) + "-"+ towStr(1 + c.get(Calendar.MONTH)) + "-"+ towStr(c.get(Calendar.DATE)); return date; } /** * �ぶ跡宒趙 * 怀� Date勤砓ㄛ殿隙 yyyy-MM-dd 跡宒 * @param date * @return */ public String dateToString(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(date); } }
最近下载更多
1358849392 LV21
2022年12月27日
moomin709 LV24
2022年7月6日
瓜不皮 LV15
2020年12月9日
kayok LV19
2019年4月17日
2469684164 LV10
2019年3月16日
落雪飞花 LV6
2018年12月7日
Joyyoe LV4
2018年10月9日
nanjing2017 LV15
2017年9月8日
hkp0521 LV7
2017年8月23日
cas LV9
2017年5月27日