java 字符串转日期格式

2023-02-22  本文已影响0人  Luke_单车

1. 将Java 字符串转化为Date

2. 将Java类里面的Date转成String

public static String getDateStrToTime(String time) {
        String timeBegin;
        //1. 字符串转化为Date
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormatYMDofyyyy);
        Date date;
        try {
            date = sdf.parse(time);

            //2. Date转成String
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatYMDofChinese);
            timeBegin = simpleDateFormat.format(date);
            Log.d("getDateStr", "timeBegin: " + timeBegin);
        } catch (ParseException e) {
            e.printStackTrace();
            timeBegin = "";
        }
        return timeBegin;

    }

* @param time 20220101
* @return 2022年01月01日
上一篇下一篇

猜你喜欢

热点阅读