首页 > 代码库 > 日期方法大全

日期方法大全

/**
     * 得到当前的时间   字符串型
     */
    public static String  getNowTime(){
        
        Date time=new Date();
        DateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today=format.format(time);
        
        return today;
    }
    /**
     * qbzuo 格式化日期 :从Date ==>String
     * 
     * @param date
     * @param style
     *            such as "yyyy-MM-dd HH:mm:ss" "yyyy-MM-dd"
     * @return
     */
    public static String formatDateWithStyle(Date date, String style) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(style);
            return format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * qbzuo 格式化日期 :从Date ==>String
     * 
     * @param date
     * @return
     */
    public static String formatDateWithLongStyle(Date date) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(DATE_STYLE_LONG);
            return format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * qbzuo 格式化日期 :从 String==>Date
     * 
     * @param date
     * @return
     */
    public static Date parseStringWithShortStyle(String datestr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(DATE_STYLE_SHORT);
            return format.parse(datestr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    /**
     * qbzuo 格式化日期 :从 String==>Date
     * 
     * @param date
     * @return
     */
    public static Date parseStringWithLongStyle(String datestr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(DATE_STYLE_LONG);
            return format.parse(datestr);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * qbzuo 格式化日期 :从Date ==>String
     * 
     * @param date
     * @return
     */
    public static String formatDateWithShortStyle(Date date) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(DATE_STYLE_SHORT);
            return format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 得到当前日期的几个月前日期
     * 
     * @return
     */
    public static String getMonthBefore(int monthCount) {
        long oneDayValue = http://www.mamicode.com/24 * 60 * 60 * 1000;
        Calendar startDate = Calendar.getInstance();

        // =========================================
        Calendar start = Calendar.getInstance();
        start.setTimeInMillis(startDate.getTimeInMillis()
                - (oneDayValue * 30 * monthCount));
        String lastMonth = ""
                + start.get(Calendar.YEAR)
                + "-"
                + ((start.get(Calendar.MONTH) + 1) < 10 ? "0"
                        + (start.get(Calendar.MONTH) + 1) : ""
                        + (start.get(Calendar.MONTH) + 1))
                + "-"
                + (start.get(Calendar.DATE) < 10 ? "0"
                        + start.get(Calendar.DATE) : ""
                        + start.get(Calendar.DATE));
        return lastMonth;
    }
/**
 * 得到指定时期的几天后的时间
 */
    public static String getDayAfter(Date date,int DayCount) {
        long oneDayValue = http://www.mamicode.com/24 * 60 * 60 * 1000;
        Calendar startDate = Calendar.getInstance();
        startDate.setTime(date);

        // =========================================
        Calendar start = Calendar.getInstance();
        start.setTimeInMillis(startDate.getTimeInMillis()
                + (oneDayValue * DayCount));
        String lastMonth = ""
                + start.get(Calendar.YEAR)
                + "-"
                + ((start.get(Calendar.MONTH) + 1) < 10 ? "0"
                        + (start.get(Calendar.MONTH) + 1) : ""
                        + (start.get(Calendar.MONTH) + 1))
                + "-"
                + (start.get(Calendar.DATE) < 10 ? "0"
                        + start.get(Calendar.DATE) : ""
                        + start.get(Calendar.DATE));
        return lastMonth;
    }
    /**
     * 得到指定时期的几天前的时间
     */
        public static String getDayBefore(Date date,int DayCount) {
            long oneDayValue = http://www.mamicode.com/24 * 60 * 60 * 1000;
            Calendar startDate = Calendar.getInstance();
            startDate.setTime(date);

            // =========================================
            Calendar start = Calendar.getInstance();
            start.setTimeInMillis(startDate.getTimeInMillis()
                    - (oneDayValue * DayCount));
            String lastMonth = ""
                    + start.get(Calendar.YEAR)
                    + "-"
                    + ((start.get(Calendar.MONTH) + 1) < 10 ? "0"
                            + (start.get(Calendar.MONTH) + 1) : ""
                            + (start.get(Calendar.MONTH) + 1))
                    + "-"
                    + (start.get(Calendar.DATE) < 10 ? "0"
                            + start.get(Calendar.DATE) : ""
                            + start.get(Calendar.DATE));
            return lastMonth;
        }
    /**
     * 得到当前日期的几个月后的日期
     * 
     * @return
     */
    public static String getMonthAfter(int monthCount) {
        long oneDayValue = http://www.mamicode.com/24 * 60 * 60 * 1000;
        Calendar startDate = Calendar.getInstance();

        // =========================================
        Calendar start = Calendar.getInstance();
        start.setTimeInMillis(startDate.getTimeInMillis()
                + (oneDayValue * 30 * monthCount));
        String lastMonth = ""
                + start.get(Calendar.YEAR)
                + "-"
                + ((start.get(Calendar.MONTH) + 1) < 10 ? "0"
                        + (start.get(Calendar.MONTH) + 1) : ""
                        + (start.get(Calendar.MONTH) + 1))
                + "-"
                + (start.get(Calendar.DATE) < 10 ? "0"
                        + start.get(Calendar.DATE) : ""
                        + start.get(Calendar.DATE));
        return lastMonth;
    }
    //获得上周日的时间  如果今天是 周五 11月30号 这得到的时间是 11月25号
    public static String getPreviousWeekSunday() {

    int    weeks=0;

        weeks--;

        int mondayPlus = getMondayPlus();

        GregorianCalendar currentDate = new GregorianCalendar();

        currentDate.add(GregorianCalendar.DATE, mondayPlus+weeks);

        Date monday = currentDate.getTime();

        DateFormat df = DateFormat.getDateInstance();

        String preMonday = df.format(monday);

        return preMonday;

        }
    //获得上周一的时间
     public static String getPreviousWeekday() {   
           int weeks=0;
           weeks--;   
            int mondayPlus = getMondayPlus();   
            GregorianCalendar currentDate = new GregorianCalendar();   
            currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);   
            Date monday = currentDate.getTime();   
            DateFormat df = DateFormat.getDateInstance();   
            String preMonday = df.format(monday);   
            return preMonday;   
        } 
    //获得本周一的时间
      public static String getMondayOFWeek(){   
          int   weeks = 0; 
          
             int mondayPlus = getMondayPlus();   
             System.out.println("mondayPlus="+mondayPlus);
             GregorianCalendar currentDate = new GregorianCalendar();   
             currentDate.add(GregorianCalendar.DATE, mondayPlus);   
             Date monday = currentDate.getTime();   
             DateFormat df = DateFormat.getDateInstance();   
             String preMonday = df.format(monday);   
             return preMonday;   
           
        }  
           
    //获得当前日期和上周日相差的天数
    private static  int getMondayPlus() {   
        Calendar cd = Calendar.getInstance();   
        // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......   
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;         //因为按中国礼拜一作为第一天所以这里减1   
       // System.out.println("dayOfWeek="+dayOfWeek);
        if (dayOfWeek == 0) {   
            return -6;   
        } else {   
            return 1 - dayOfWeek;   
        }   
    }   
  //  获得本周日的日期   如果今天是 周五 11月30号 这得到的时间是 12月2号
    public static String getCurrentWeekday() {   
       int weeks = 0;   
        int mondayPlus = getMondayPlus();   
        GregorianCalendar currentDate = new GregorianCalendar();   
        currentDate.add(GregorianCalendar.DATE, mondayPlus+6);   
        Date monday = currentDate.getTime();   
           
        DateFormat df = DateFormat.getDateInstance();   
        String preMonday = df.format(monday);   
        return preMonday;   
    }   
       
//获得当前日期是本月的第一天
public static String  getFirstDayOfThisMonth() {
    Calendar cd=Calendar.getInstance();
    int dayOfMonth=cd.get(Calendar.DAY_OF_MONTH);
    return getDayBefor(dayOfMonth-1);
}
//获得当前日期上月的第一天
public static String  getFirstDayOfLastMonth() {
    Calendar cd=Calendar.getInstance();
    int month=cd.get(Calendar.MONTH)+1;
    int year=cd.get(Calendar.YEAR);
    int lastMonth=1;
    int lastYear=2000;
    if (month==12) {
        lastMonth=1;
        lastYear=year-1;
    }else {
        lastMonth=month-1;
        lastYear=year;
    }
    
    return lastYear+"-"+(lastMonth<10?("0"+lastMonth):lastMonth)+"-01";
}
//获得当前日期上月的最后一天    
public static String  getLastDayOfLastMonth() {
    Calendar cd=Calendar.getInstance();
    int month=cd.get(Calendar.MONTH)+1;
    int year=cd.get(Calendar.YEAR);
    int lastMonth=1;
    int lastYear=2000;
    int lastDay=30;
    if (month==12) {
        lastMonth=1;
        lastYear=year-1;
    }else {
        lastMonth=month-1;
        lastYear=year;
    }
    
    if (lastMonth==2) {
        lastDay=28;
    }else if (lastMonth==4||lastMonth==6||lastMonth==9||lastMonth==11) {
        lastDay=30;
    }else {
        lastDay=31;
    }
    return lastYear+"-"+(lastMonth<10?("0"+lastMonth):lastMonth)+"-"+lastDay;
}
    // 得到几天前的日期
    public static String getDayBefor(int dateCount) {
        long oneDayValue = http://www.mamicode.com/24 * 60 * 60 * 1000;
        Calendar startDate = Calendar.getInstance();

        // =========================================
        Calendar start = Calendar.getInstance();
        start.setTimeInMillis(startDate.getTimeInMillis()
                - (oneDayValue * dateCount));
        String lastMonth = ""
                + start.get(Calendar.YEAR)
                + "-"
                + ((start.get(Calendar.MONTH) + 1) < 10 ? "0"
                        + (start.get(Calendar.MONTH) + 1) : ""
                        + (start.get(Calendar.MONTH) + 1))
                + "-"
                + (start.get(Calendar.DATE) < 10 ? "0"
                        + start.get(Calendar.DATE) : ""
                        + start.get(Calendar.DATE));
        return lastMonth;

    }