Conversion and the date string

/ **
        * date conversion character string
        * @param date Date
        * @param type display format of the MM-dd YYYY-YYYY the MM-dd-HH: mm: SS
        * = UtilClass.DateToString ReturnValue String (this.Startdatetest.getValue (), "the MM-dd-YYYY");
        * /

public static String dateToString(Object date,String type) {
        String retultValue = "";
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(type);

// It should be noted that if you pass a value of type String, the need to convert again, format inside the parameter is Date type, otherwise there will be java.lang.IllegalArgumentException: Can not format given Object as a Date abnormal.

              //retultValue = sdf.format(sdf.parse(date));
                retultValue = sdf.format(date);
            } catch (Exception e) {
                 e.printStackTrace();
            }
        }
        return retultValue;
    }

 

/**
     * 字符串转为日期
     * @param DateStr 字符串
     * @param type 类型 "yyyy-MM-dd HH:mm:ss"
     * @return Date  java.util.Date
     */
    public static Date StringToDate(String date,String type) {
        Date resultDate = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(type);
            resultDate = sdf.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDate;     
    }

static void main public (String [] args) {
        
        String type = "the MM-dd-YYYY HH: mm: SS"; // Date Format
        long date = 1559014035000L; // stamp

           // Get the current time

         // long date = System.currentTimeMillis();

        // Date date = new Date();
//        Date riqi = StringToDate(date, type);
//        System.out.println(riqi);
        String shiJian = dateToString(date, type);
        System.out.println(shiJian);
    }

Guess you like

Origin blog.csdn.net/a754315344/article/details/90668341