java Date日期总结的一些转换的方法

场景:java中Date日期总结的一些转换的方法(参考别人的)

代码:

public class DateUtil {
    /** 
     * 时间戳转换成日期格式字符串 
     * @param seconds 精确到秒的字符串 
     * @param formatStr 
     * @return 
     */  
    public static String timeStamp2Date(String seconds,String format) {  
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){  
            return "";  
        }  
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }   
        SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(new Date(Long.valueOf(seconds+"000")));  
    }  
    /** 
     * 日期格式字符串转换成时间戳 
     * @param date 字符串日期 
     * @param format 如:yyyy-MM-dd HH:mm:ss 
     * @return 
     */  
    public static String date2TimeStamp(String date_str,String format){  
        try {  
            SimpleDateFormat sdf = new SimpleDateFormat(format);  
            return String.valueOf(sdf.parse(date_str).getTime()/1000);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return "";  
    }  
      
    /** 
     * 取得当前时间戳(精确到秒) 
     * @return 
     */  
    public static String timeStamp(){  
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);  
        return t;  
    }  

    /**
     * 根据当前时间,得到前几个月的时间
     * @param setMon 设置前几个月的数值
     * @return
     */
    public static String getBeforeDataByCurrent(int setMon){
        Date curDate = new Date();//当前时间
        Date threeMonDate = new Date();
        Calendar calendar = Calendar.getInstance();//得到日历
        calendar.setTime(curDate);//把当前时间赋给日历
        calendar.add(Calendar.MONTH, setMon);//设置为前3月  如:“-3”
        threeMonDate = calendar.getTime(); //得到前3月的时间
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");//设置时间格式
        String threeMonDateStr = sdf.format(threeMonDate);//格式化前3月的时间
        return threeMonDateStr;
    }

  /**
* 获取当前的日期
* @return
*/
   public static String getCurrentDate(){  
      Date current = new Date(System.currentTimeMillis());
      String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(current);
      return currentDate;
   }


    /**
    * 获取当前的日期
    * @return
    */
    public static String getYesterDay(){
      Date yesterDay = new Date(System.currentTimeMillis() - 86400000L);
      String yesterDate = new SimpleDateFormat("yyyy-MM-dd").format(yesterDay);
      return yesterDate;
    }

    
public static void main(String[] args) { //传入的时间戳 String timeStamp = "1569427200"; //定义将时间戳转换的日期的格式 String date = timeStamp2Date(timeStamp, "yyyy-MM-dd"); System.out.println("根据时间戳(1569427200)获取到的Date:"+date);//运行输出:date=2016-08-04 10:34:42 int setMon = -3; String dateStr = getBeforeDataByCurrent(setMon);//根据当前时间,获取到三个月前的时间戳 String timeStamp2 = date2TimeStamp(dateStr, "yyyyMMdd"); //根据当前时间,获取到三个月前的时间戳 System.out.println("三个月之前的时间戳:"+timeStamp2); String threeDate = timeStamp2Date(timeStamp2, "yyyy-MM-dd"); System.out.println("三个月之前的日期:"+threeDate); } }
原文地址:https://www.cnblogs.com/liangxiaojin/p/12463391.html