java中时间处理

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BBDateUtil {
    private static String WITHOUTTIME_DATE_PATTERN = "yyyyMMdd";
    public static String ZH_CN_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static String year_month_day = "yyyy-MM-dd";
    public static String  yyyyMMddHHmmss= "yyyyMMddHHmmss";

    // 获取"yyyyMMdd"格式时间
    public static String getCurrentDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(WITHOUTTIME_DATE_PATTERN);
        String time = sdf.format(date);
        return time;
    }

    // 获取"yyyy-MM-dd"格式时间
    public static String getCurrentDatetoYear_month_day() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(year_month_day);
        String time = sdf.format(date);
        return time;
    }

    // 获取"yyyy-MM-dd HH:mm:ss"格式时间
    public static String getCurrentDateTime() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(ZH_CN_DATETIME_PATTERN);
        String time = sdf.format(date);
        return time;
    }
    //获取"yyyyMMdd"格式时间
     public static String getCurrentDateyyyyMMddHHmmss(){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss);
            String time = sdf.format(date);
            return time;
        }

    // 获取"yyyyMMdd"格式时间并在当前月份上面更改月份
    public static String getCurrentDateChangeMonth(int month) {
        SimpleDateFormat dft = new SimpleDateFormat(WITHOUTTIME_DATE_PATTERN);
        Calendar date = Calendar.getInstance();
        date.add(Calendar.MONTH, month);
        String endDate = dft.format(date.getTime());
        return endDate;
    }

    // 获取"yyyy_MM_dd"格式时间并在当前月份上面更改月份
    public static String getCurrentYear_month_dayChangeMonth(int month) {
        SimpleDateFormat dft = new SimpleDateFormat(year_month_day);
        Calendar date = Calendar.getInstance();
        date.add(Calendar.MONTH, month);
        String endDate = dft.format(date.getTime());
        return endDate;
    }

    // 获取"yyyy-MM-dd HH:mm:ss"格式时间并在当前月份上面更改月份
    public static String getCurrentDateTimeChangeMonth(int month) {
        SimpleDateFormat dft = new SimpleDateFormat(ZH_CN_DATETIME_PATTERN);
        Calendar date = Calendar.getInstance();
        date.add(Calendar.MONTH, month);
        String endDate = dft.format(date.getTime());
        return endDate;
    }

    // 获取"yyyy-MM-dd HH:mm:ss"格式时间,变更小时
    public static String getCurrentDateTimeChangeHour(int hour) {
        SimpleDateFormat sdf = new SimpleDateFormat(ZH_CN_DATETIME_PATTERN);
        Calendar date = Calendar.getInstance();
        date.add(Calendar.HOUR_OF_DAY, hour);
        String time = sdf.format(date.getTime());
        return time;
    }

    // 获取"yyyy-MM-dd HH:mm:ss"格式时间改变天数
    public static String getCurrentDateTimeChangeDay(int day) {
        SimpleDateFormat dft = new SimpleDateFormat(ZH_CN_DATETIME_PATTERN);
        Calendar date = Calendar.getInstance();
        date.add(Calendar.DAY_OF_MONTH, day);
        String endDate = dft.format(date.getTime());
        return endDate;
    }

    // 获取"yyyy-MM-dd"格式时间改变天数
    public static String getCurrent_year_month_day_ChangeDay(int day) {
        SimpleDateFormat dft = new SimpleDateFormat(year_month_day);
        Calendar date = Calendar.getInstance();
        date.add(Calendar.DAY_OF_MONTH, day);
        String endDate = dft.format(date.getTime());
        return endDate;
    }

    // 获取"yyyyMMdd"格式时间改变天数
    public static String getCurrentDateChangeDay(int day) {
        SimpleDateFormat dft = new SimpleDateFormat(WITHOUTTIME_DATE_PATTERN);
        Calendar date = Calendar.getInstance();
        date.add(Calendar.DAY_OF_MONTH, day);
        String endDate = dft.format(date.getTime());
        return endDate;
    }

    // 指定"yyyy-MM-dd"格式时间并改变添加、减少指定月份
    public static String DesignatedDateChangeMonth(String datetime, int month) {
        try {
            Date dft = new SimpleDateFormat(WITHOUTTIME_DATE_PATTERN).parse(datetime);
            SimpleDateFormat simdate = new SimpleDateFormat(WITHOUTTIME_DATE_PATTERN);
            Calendar date = Calendar.getInstance();
            date.setTime(dft);
            date.add(Calendar.MONTH, month);
            String endDate = simdate.format(date.getTime());
            return endDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 指定"yyyy-MM-dd"格式时间并改变添加、减少指定月份
    public static String Designated_year_month_day_ChangeMonth(String datetime,
            int month) {
        try {
            Date dft = new SimpleDateFormat(year_month_day).parse(datetime);
            SimpleDateFormat simdate = new SimpleDateFormat(year_month_day);
            Calendar date = Calendar.getInstance();
            date.setTime(dft);
            date.add(Calendar.MONTH, month);
            String endDate = simdate.format(date.getTime());
            return endDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 指定"yyyy-MM-dd"格式时间并改变添加、减少指定月份
    public static String DesignatedDateTimeChangeMonth(String datetime,
            int month) {
        try {
            Date dft = new SimpleDateFormat(ZH_CN_DATETIME_PATTERN)
                    .parse(datetime);
            SimpleDateFormat simdate = new SimpleDateFormat(
                    ZH_CN_DATETIME_PATTERN);
            Calendar date = Calendar.getInstance();
            date.setTime(dft);
            date.add(Calendar.MONTH, month);
            String endDate = simdate.format(date.getTime());
            return endDate;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    
    // 指定"yyyy-MM-dd"格式时间并改变添加、减少指定日
        public static String DesignatedDateTimeChangeDay(String datetime,int day) {
            try {
                Date dft = new SimpleDateFormat(ZH_CN_DATETIME_PATTERN).parse(datetime);
                SimpleDateFormat simdate = new SimpleDateFormat(ZH_CN_DATETIME_PATTERN);
                Calendar date = Calendar.getInstance();
                date.setTime(dft);
                date.add(Calendar.DAY_OF_MONTH, day);
                String endDate = simdate.format(date.getTime());
                return endDate;
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    public static int getDayD_VALUE(String begin,String end){
        try {
            Date from = new SimpleDateFormat(ZH_CN_DATETIME_PATTERN).parse(begin);
            Date to =   new SimpleDateFormat(ZH_CN_DATETIME_PATTERN).parse(end);
            int day=(int) ((to.getTime()-from.getTime())/(1000*60*60*24));
            return day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    public static int getDayD_VALUE_year_month_day(String begin,String end){
        try {
            Date from = new SimpleDateFormat(year_month_day).parse(begin);
            Date to =   new SimpleDateFormat(year_month_day).parse(end);
            int day=(int) ((to.getTime()-from.getTime())/(1000*60*60*24));
            return day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    public static int getDayyyyyMMddHHmmssD_VALUE(String begin,String end){
        try {
            Date from = new SimpleDateFormat(yyyyMMddHHmmss).parse(begin);
            Date to =   new SimpleDateFormat(yyyyMMddHHmmss).parse(end);
            int day=(int) ((to.getTime()-from.getTime())/(1000*60));
            return day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public static String changeTimestampToSimpleDate(String timestamp,String geshi){
        Date date = new Date(Long.parseLong(timestamp));
        SimpleDateFormat ss = new SimpleDateFormat(geshi);
        String format = ss.format(date);
        return format;
    }
    


    public static void main(String[] args) {
        

//         System.out.println(BBDateUtil.getCurrent_year_month_day_ChangeDay(1));
//         System.out.println(BBDateUtil.getCurrentYear_month_dayChangeMonth(1));
//        System.out.println(BBDateUtil.Designated_year_month_day_ChangeMonth("2017-05-10",1));
//        System.out.println(BBDateUtil.getDayD_VALUE("2017-07-12 12:50:17","2017-07-05 00:00:00"));
//        System.out.println(BBDateUtil.getDayD_VALUE_year_month_day("2017-07-12","2017-07-15"));
//        System.out.println(BBDateUtil.getDayyyyyMMddHHmmssD_VALUE("20170615163142","20170615163144"));
//        System.out.println(BBDateUtil.getCurrentYear_month_dayChangeMonth(1));
//        System.out.println(BBDateUtil.getCurrentDatetoYear_month_day());
//        System.out.println(BBDateUtil.changeTimestampToSimpleDate("1502362822000","yyyy-MM-dd HH:mm:ss"));
    }

}

 

posted @ 2017-11-16 09:43  hct118  阅读(276)  评论(0编辑  收藏  举报