本文转载自:http://blog.csdn.net/xuduzhoud/article/details/27526177
全部代码如下:
DateUtil.java
package com.example.tset; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 日期操作工具类. * * @author shimiso * http://blog.csdn.net/xuduzhoud/article/details/27526177 */ public class DateUtil { private static final String FORMAT = "yyyy-MM-dd HH:mm:ss"; public static Date str2Date(String str) { return str2Date(str, null); } public static Date str2Date(String str, String format) { if (str == null || str.length() == 0) { return null; } if (format == null || format.length() == 0) { format = FORMAT; } Date date = null; try { SimpleDateFormat sdf = new SimpleDateFormat(format); date = sdf.parse(str); } catch (Exception e) { e.printStackTrace(); } return date; } public static Calendar str2Calendar(String str) { return str2Calendar(str, null); } public static Calendar str2Calendar(String str, String format) { Date date = str2Date(str, format); if (date == null) { return null; } Calendar c = Calendar.getInstance(); c.setTime(date); return c; } public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss return date2Str(c, null); } public static String date2Str(Calendar c, String format) { if (c == null) { return null; } return date2Str(c.getTime(), format); } public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss return date2Str(d, null); } public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss if (d == null) { return null; } if (format == null || format.length() == 0) { format = FORMAT; } SimpleDateFormat sdf = new SimpleDateFormat(format); String s = sdf.format(d); return s; } public static String getCurDateStr() { Calendar c = Calendar.getInstance(); c.setTime(new Date()); return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "-" + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND); } /** * 获得当前日期的字符串格式 * * @param format * @return */ public static String getCurDateStr(String format) { Calendar c = Calendar.getInstance(); return date2Str(c, format); } // 格式到秒 public static String getMillon(long time) { return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time); } // 格式到天 public static String getDay(long time) { return new SimpleDateFormat("yyyy-MM-dd").format(time); } // 格式到毫秒 public static String getSMillon(long time) { return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time); } /* 输入的是String,格式诸如20120102,实现加一天的功能,返回的格式为String,诸如20120103 */ public static String stringDatePlus(String row) throws ParseException{ String year=row.substring(0, 4); String month=row.substring(4,6); String day=row.substring(6); String date1=year+"-"+month+"-"+day; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date startDate=sdf.parse(date1); Calendar cd = Calendar.getInstance(); cd.setTime(startDate); cd.add(Calendar.DATE, 1); String dateStr =sdf.format(cd.getTime()); String year1=dateStr.substring(0,4); String month1=dateStr.substring(5,7); String day1=dateStr.substring(8); return year1+month1+day1; } /* 输入的是String,格式诸如20120102,实现减一天的功能,返回的格式为String,诸如20120101 */ public static String stringDateDecrease(String row) throws ParseException{ String year=row.substring(0, 4); String month=row.substring(4,6); String day=row.substring(6); String date1=year+"-"+month+"-"+day; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Date startDate=sdf.parse(date1); Calendar cd = Calendar.getInstance(); cd.setTime(startDate); cd.add(Calendar.DATE, -1); String dateStr =sdf.format(cd.getTime()); String year1=dateStr.substring(0,4); String month1=dateStr.substring(5,7); String day1=dateStr.substring(8); return year1+month1+day1; } /* 输入的格式为String,诸如20120101,返回的格式为String,诸如2012-01-01 */ public static String stringDateChange(String date) { if(date.length()=="20120101".length()){ String year=date.substring(0, 4); String month=date.substring(4,6); String day=date.substring(6); return year+"-"+month+"-"+day; }else{ return date; } } /** * 日期向后推一天 * @param date 格式:20120101 * @return 20120102 */ public static String tonextday(String date){ int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day+1); Date newdate = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String da = format.format(newdate); return da; } /** * 获取当前日期上一周的开始日期 (周日) */ public static String previousWeekByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值 cal.add(Calendar.WEEK_OF_YEAR, -1); String imptimeBegin = sdf.format(cal.getTime()); // System.out.println("所在周星期日的日期:"+imptimeBegin); return imptimeBegin; } /** * 获取当前日期上一周的结束日期 (周六) */ public static String previousWeekEndDayByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s)); cal.add(Calendar.WEEK_OF_YEAR, -1); String imptimeBegin = sdf.format(cal.getTime()); // System.out.println("星期六的日期:"+imptimeBegin); return imptimeBegin; } /** * 获取当前日期当前一周的开始日期 (周日) */ public static String getCurrentWeekFirstDayByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//根据日历的规则,给当前日期减往星期几与一个星期第一天的差值 String imptimeBegin = sdf.format(cal.getTime()); // System.out.println("所在周星期日的日期:"+imptimeBegin); return imptimeBegin; } /** * 获取当前日期当前一周的结束日期 (周六) */ public static String getCurrentWeekEndDayByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, day); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 if(1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s)); String imptimeBegin = sdf.format(cal.getTime()); return imptimeBegin; } /** * 返回上一个月的第一天 * @param date20120304 * @return 20120201 */ public static String previousMonthByDate(String date) { // TODO Auto-generated method stub int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-2, 1); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); String imptimeBegin = sdf.format(cal.getTime()); // System.out.println(imptimeBegin); return imptimeBegin; } /** * 返回下一个月的第一天 * @param date20120304 * @return 20120401 */ public static String nextMonthByDate(String date) { // TODO Auto-generated method stub int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month, 1); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); String imptimeBegin = sdf.format(cal.getTime()); // System.out.println(imptimeBegin); return imptimeBegin; } /** * 返回当前月的第一天 * @param date20120103 * @return 20120101 */ public static String getCurrentMonthFirstDayByDate(String date) { int year = Integer.parseInt(date.substring(0,4)); int month = Integer.parseInt(date.substring(4,6)); int day = Integer.parseInt(date.substring(6)); Calendar calendar = Calendar.getInstance(); calendar.set(year, month-1, 1); Date newdate = calendar.getTime(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); Calendar cal = Calendar.getInstance(); cal.setTime(newdate); String imptimeBegin = sdf.format(cal.getTime()); return imptimeBegin; } }