Android Format date with time zone

前端 未结 8 1372
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 13:05

I need to format the date into a specific string.

I used SimpleDateFormat class to format the date using the pattern \"yyyy-MM-dd\'T\'HH:m

8条回答
  •  误落风尘
    2020-12-10 13:42

    Simply refactor this code and replace the 'Locale.getDefault()' with the Locale you need

    private SimpleDateFormat getDateFormat() {
        SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.DEFAULT, Locale.getDefault());
        String pattern = dateFormat.toLocalizedPattern();
        pattern = pattern.trim();
        dateFormat.applyLocalizedPattern(pattern);
        return dateFormat;
    }
    

    //And here is the usage

    SimpleDateFormat sdf = getDateFormat();

    sdf.format(new Date());

提交回复
热议问题