將yyyyMMdd轉成yyyy-MM-dd格式和yyyy-MM-dd格式轉成yyyyMMdd

格式轉換代碼

    /**
     *@Description:日期轉換,將接口返回的20180524轉爲2018-05-24
     *@author haohaounique
     *@Date 2018年5月24日
     *@param str 傳遞的日期字符串
     *@return
     *@exception :異常返回null,保障數據庫的數據一致性,數據庫格式yyyyMMdd
     */
    private static String dateConvertion(String str) {
        Date parse = null;
        String dateString = "";
        try {
            parse = new SimpleDateFormat("yyyyMMdd").parse(str);
            dateString = new SimpleDateFormat("yyyy-MM-dd").format(parse);
        } catch (ParseException e) {
            dateString=null;
        }
        
        return dateString;
    }

/**
     *@Description:日期轉換,將前臺的2018-05-24轉爲20180524
     *@author haohaounique
     *@Date 2018年5月24日
     *@param str 傳遞的日期字符串
     *@return
     *@exception :異常返回null,保障數據庫的數據一致性,數據庫格式yyyyMMdd
     */
    private static String StringToDate(String str) {
        Date parse = null;
        String dateString = "";
        try {
            parse=new SimpleDateFormat("yyyy-MM-dd").parse(str);
            dateString = new SimpleDateFormat("yyyyMMdd").format(parse);
        } catch (ParseException e) {
            dateString=null;
        }
        return dateString;
    }

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章