常用操作日期和操作时间戳方法,拿着直接用系列

package com.gxw.util;

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

public class DateTime {

    //日期时间24小时格式字符串
    public final static String formatDateTime24H = "yyyy-MM-dd HH:mm:ss";

    //日期时间12小时格式字符串
    public final static String formatDateTime12H = "yyyy-MM-dd hh:mm:ss";

    //日期格式字符串
    public final static String formatDate = "yyyy-MM-dd";

    //时间24格式字符串
    public final static String formatTime24H = "HH:mm:ss";

    //时间12格式字符串
    public final static String formatTime12H = "hh:mm:ss";

    /**
     * 获取当前日期戳 yyyy-MM-dd hh:mm:ss
     * @return
     */
    public static String getNowDateStamp(){
        return String.valueOf(new Date().getTime());
    }

    /**
     * 获取当前日期字符串 yyyy-MM-dd hh:mm:ss
     * @return
     */
    public static String getNowDateStr(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return sdf.format(date);
    }

    /**
     * 时间戳转日期
     * @param stamp
     * @param format
     * @return
     */
    public static String getStampCaseDate(String stamp, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long stp = new Long(stamp);
        Date date = new Date(stp);
        String str = sdf.format(date);
        return str;
    }

    /**
     * 时间戳转日期
     * @param stamp
     * @return
     */
    public static String getStampCaseDateFor24(String stamp){
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long stp = new Long(stamp);
        Date date = new Date(stp);
        String str = sdf.format(date);
        return str;
    }

    /**
     * 字符串日期转换时间戳
     * @param date
     * @return
     */
    public static String getDateCaseStamp(String date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d = sdf.parse(date);
            return String.valueOf(d.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 字符串日期转换时间戳
     * @param date
     * @return
     */
    public static String getDateStrCaseStamp(String date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date d = sdf.parse(date);
            return String.valueOf(d.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取当周的周一日期
     * @return
     */
    public static String getNowWeekStartStamp(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        //获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }

        // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
        Date mondayDate = cal.getTime();
        mondayDate.setHours(00);
        mondayDate.setMinutes(00);
        mondayDate.setSeconds(00);
        String weekBegin = sdf.format(mondayDate);
//        System.out.println("所在周星期一的日期:" + weekBegin);
        return String.valueOf(mondayDate.getTime());
    }

    /**
     * 获取当周的周天日期
     * @return
     */
    public static String getNowWeekEndStamp(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        //设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        //获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if(dayWeek==1){
            dayWeek = 8;
        }
        // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
        cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
        Date sundayDate = cal.getTime();
        sundayDate.setHours(23);
        sundayDate.setMinutes(59);
        sundayDate.setSeconds(59);
        String weekEnd = sdf.format(sundayDate);
//        System.out.println("所在周星期日的日期:" + weekEnd);
        return String.valueOf(sundayDate.getTime()).substring(0,10) + "999";
    }

    /**
     * 将当天时间戳转换成当天最大时间的时间戳
     * @param stamp
     * @return
     */
    public static String getStampMaxDate(String stamp){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(Long.parseLong(stamp));
        String dateStr = sdf.format(date) + " 23:59:59";
        String rsStamp = getDateCaseStamp(dateStr);
        return rsStamp;
    }

    /**
     * 获取当前时间的两月前时间戳
     * @return
     */
    public static String getStamp2MonthBeforeDate(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);//设置当前月的一号
        calendar.add(Calendar.MONTH, -2); //0表示当前月,-2就是当前月-2
        calendar.set(Calendar.HOUR_OF_DAY, 0);//设置当天几时
        calendar.set(Calendar.MINUTE, 0);//设置当天几分
        calendar.set(Calendar.SECOND, 0);//设置当天几秒
        calendar.set(Calendar.MILLISECOND, 0);//设置当天几毫秒
        long t = calendar.getTimeInMillis();
        String s = sdf.format(new Date(t));
        System.out.println(t);
        System.out.println(s);
        return String.valueOf(calendar.getTimeInMillis());
    }
    
    /**
     * 校验日期是否合法
     * @return
     */
    public static boolean isValidDate(String s) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            fmt.parse(s);
            return true;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
            return false;
        }
    }
    
    /**
     * <li>功能描述:时间相减得到天数
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long 
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr,String endDateStr){
        long day=0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = null;
        Date endDate = null;
        
            try {
                beginDate = format.parse(beginDateStr);
                endDate= format.parse(endDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
            //System.out.println("相隔的天数="+day);
      
        return day;
    }
    
    /**
     * 得到n天之后的日期
     * @param days
     * @return
     */
    public static String getAfterDayDate(String days) {
        int daysInt = Integer.parseInt(days);
        
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        
        return dateStr;
    }
    
    /**
     * 得到n天之后是周几
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
     * 得到 n 小时之后的时间戳
     * @param afterHours    //后几个小时
     */
    public static String getAfterHours(int afterHours){
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.HOUR, afterHours);
        Date date = canlendar.getTime();
        return String.valueOf(date.getTime());
    }

    /**
     * 得到 n 小时之前的时间戳
     * @param beforeHours    //前几个小时
     */
    public static String getBeforeHours(int beforeHours){
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.HOUR, -beforeHours);
        Date date = canlendar.getTime();
        return String.valueOf(date.getTime());
    }

}

稳之楠
130 声望25 粉丝

行之稳,为之楠!