package cn.com.free.util; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class DateUtil { /** * 取得当前年份 * * @author zbq * @date 2010-01-29 */ public static String getCurrentYear(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); Date date = new Date(); return sdf.format(date); } /** * 取得年月日时分秒 * * @author zbq * @date 2010-01-29 */ public static String getCurrentTime(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); return sdf.format(date); } /** * 取得年月日时分秒 * * @author zbq * @date 2010-01-29 */ public static String getYmdSfm(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); Date date = new Date(); return sdf.format(date); } /** * 取得当前日期字符串 * * @return YYYYMMDD(例:20110829) * * @Author zbq * @Date 2011-8-30 */ public static String getTodayYmd() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); return sdf.format(date); } /** * 取得一周日期 * * @author zbq * @date 2010-01-29 */ public static List getDateToWeek(Date date){ List dateWeekList = new ArrayList(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String time = ""; //flag用来存取与当天日期的相差数 int flag = 0; for(int i=1;i<8;i++){ //新建日历 Calendar cal = Calendar.getInstance(); //在日历中找到当前日期 cal.setTime(date); //当前日期时本周第几天,默认按照西方惯例上周星期天为第一天 flag = -cal.get(Calendar.DAY_OF_WEEK); //根据循环。当天与上周星期天和本周一到周五相差的天数 cal.add(Calendar.DATE, flag+i); //转化格式 time = sdf.format(cal.getTime()); //存入list dateWeekList.add(time); } return dateWeekList; } /** * 两个日期比较 * * @return 比较结果0为相等、小于0为前小于后,大于0为前大于后 * * @Author zbq * @Date 2011-8-30 */ public static int getDateTimeCompare(String dateStrStart,String dateStrEnd) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { c2.setTime(sdf.parse(dateStrEnd)); c1.setTime(sdf.parse(dateStrStart)); } catch (java.text.ParseException e) { } int result = c1.compareTo(c2); return result; } }