Date 时间 日期 常用方法函数

1、描述:Date类型转化为String类型.

@param date the dateo

@param format 类型如 yyyy-MM-dd HH:mm:ss 

    public static String getStringByFormat(Date date, String format) {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        String strDate = null;
        try {
            strDate = mSimpleDateFormat.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }

2、获取当前时间 

pattern 类型 yyyy-MM-dd HH:mm:ss

    public static String getCurrentDate(String pattern) {

        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        String timestamp = formatter.format(curDate);

        return timestamp;
    }

3、String转为Date

strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日

HH时mm分ss秒

strTime的时间格式必须要与formatType的时间格式相同

    public static Date stringToDate(String checktime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(checktime);
        return date;
    }

猜你喜欢

转载自www.cnblogs.com/zhaozilongcjiajia/p/10484762.html