DateUtil

public class DateUtil {
    /** * 规范化日期,规范成yyyy-MM-dd HH:mm:ss * @param timestamp * @return */
    public static String timestamp2Datetime(long timestamp){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(new Date(timestamp * 1000));
    }
    /** * 规范化日期,规范成yyyy-MM-dd * @param timestamp * @return */
    public static String timestamp2Date(long timestamp){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(new Date(timestamp * 1000));
    }
    /** * 规范化日期,规范成yyyy-MM-dd HH:mm:ss * @param date * @return */
    public static String Date2DateString(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }
    /** * 规范化日期,规范成yyyy-MM-dd * @param date * @return */
    public static String Date2String(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }
    public static int Stringdate2Intdate(String date){
        String [] str = date.split("-");
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<str.length;i++){
            sb.append(str[i]);
        }

        return Integer.valueOf(sb.toString());
    }
    /** * yyyyMMdd转成long型数据 * @param datestr * @return */
    public static long date3Timestamp(String datestr){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = dateFormat.parse(datestr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long timestamp = date.getTime()/1000;
        return timestamp;
    }

    /** *获得当前时间 * @return */
    public static String getCurrentDatetime(){
        Date date=new Date();
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }


    /** * 获得当前日期 * @return */
    public static String getCurrentFormatDate2(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return dateFormat.format(date);
    }

    /** * 日期相加,并按照要求日期格式输出 ,示例:20120305 * @param date 被操作日期 * @param days 被加天数 * @return 标准化操作后日期 */
    public static String addDate(java.util.Date date,int days){
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        java.text.SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        calendar.setTimeInMillis(getMillis(date) +  ((long)  days)  *  24  *  3600  *  1000);
        return simpleDateFormat.format(calendar.getTime());
    }

    /** * 日期相减,并按照要求日期格式输出 ,示例:20120305 * @param date 被操作日期 * @param days 被加天数 * @return 标准化操作后日期 */
    public static String diffDate(java.util.Date date,int days){
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        java.text.SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        calendar.setTimeInMillis(getMillis(date) - ((long)  days)  *  24  *  3600  *  1000);
        return simpleDateFormat.format(calendar.getTime());
    }
    /** * 返回毫秒数 * @param date 日期 * @return 返回毫秒 */
    public static long getMillis(java.util.Date date){
        java.util.Calendar calendar = java.util.Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getTimeInMillis();
    }
    /** * 获取上个月第一天 */
    public static String getFirstDayofLastMonth(int day){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -day);
        Date thedate = c.getTime();

        Calendar l = Calendar.getInstance();
        l.setTime(thedate);
        l.set(Calendar.DAY_OF_MONTH, 1);
        String firstday = dateFormat.format(l.getTime());
        return firstday;
    }
    /** * 获取本月第一天 * @return */
    public static String getFirstDayOfThisMonth(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
        String firstday = dateFormat.format(c.getTime());
        return firstday;
    }

    public static String getYesterday(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -1);
        String firstday = dateFormat.format(c.getTime());
        return firstday;
    }

    public static String getCurrentYearMonth(String date){
        SimpleDateFormat df=new SimpleDateFormat("yyyyMM");
        SimpleDateFormat df1=new SimpleDateFormat("yyyy-MM-dd");
        Date dates = new Date();
        try {
            dates =df1.parse(date);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return df.format(dates);
    }

    public static void main(String []args){
        System.out.println(getFirstDayOfThisMonth());
        System.out.println(getFirstDayofLastMonth(3));
        System.out.println(getYesterday());
        System.out.println(getCurrentFormatDate2());
        System.out.println(getCurrentYearMonth("2014-09-01"));
        System.out.println(DateUtil.getFirstDayofLastMonth(2));
    }
}

你可能感兴趣的:(DateUtil)