Java利用正则表达式实现中英文日期转换函数封装

前言: 

Java利用正则表达式实现中英文日期互相转换函数封装,一共实现了6个方法(封装的原因是因为在Android中使用SimpleDateFormat 太麻烦了,各种try catch exception 特别麻烦,但是如果使用JDK8的LocalDate,则失去了对低版本OS的支持(jdk8最少要安卓8.0+才能支持)加上又不愿意使用第三方的日期包,so...)

以下是将 "yyyy-MM-dd" 转换为 "yyyy年M月d日"  或将 "yyyy-MM" 转换为 "yyyy年M月" 的示例代码已实现了相互转换,简单好用。

使用 %d 和 %02d 格式符将年份和月份格式化为不带前导零和带前导零的字符串。该代码首先使用正则表达式将输入的日期字符串匹配出年、月、日三个部分,然后将月份和日期字符串使用 Integer.parseInt() 方法转换为整数,最后使用 %02d 格式符将月份和日期格式化为两位数的字符串,自动补齐前导零。如果转为中文日期则去掉前导零。 

//带前导零    
public static String convertDateToChs(String dateStr) {
        String regex = "(\\d{4})-(\\d{1,2})-(\\d{1,2})";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dateStr);
        if (matcher.matches()) {
            String year = matcher.group(1);
            String month = matcher.group(2);
            String day = matcher.group(3);
            return String.format("%s年%s月%s日", year, month, day);
        } else {
            return dateStr;
        }
    }

//不带前导零
    public static String convertToChinese(String dateStr) {
        String regex = "(\\d{4})-(\\d{1,2})-(\\d{1,2})";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dateStr);
        if (matcher.matches()) {
            int year = Integer.parseInt(matcher.group(1));
            int month = Integer.parseInt(matcher.group(2));
            int day = Integer.parseInt(matcher.group(3));
            return String.format("%d年%d月%d日", year, month, day);
        } else {
            return dateStr;
        }
    }

    public static String convertDateToEn(String dateStr) {
        String regex = "(\\d{4})年(\\d{1,2})月(\\d{1,2})日";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dateStr);
        if (matcher.matches()) {
            String year = matcher.group(1);
            String month = matcher.group(2);
            String day = matcher.group(3);
            return String.format("%s-%s-%s", year, month, day);
        } else {
            return dateStr;
        }
    }

    //使用 String.format() 方法的 %02d 格式符,将月份和日期格式化为两位数的字符串,自动补齐前导零
    public static String convertDateToEnglish(String dateStr) {
        String regex = "(\\d{4})年(\\d{1,2})月(\\d{1,2})日";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dateStr);
        if (matcher.matches()) {
            int year = Integer.parseInt(matcher.group(1));
            int month = Integer.parseInt(matcher.group(2));
            int day = Integer.parseInt(matcher.group(3));
            return String.format("%d-%02d-%02d", year, month, day);
        } else {
            return dateStr;
        }
    }

    public static String convertToShortChinese(String dateStr) {
        String regex = "(\\d{4})-(\\d{1,2})";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dateStr);
        if (matcher.matches()) {
            int year = Integer.parseInt(matcher.group(1));
            int month = Integer.parseInt(matcher.group(2));
            return String.format("%d年%d月", year, month);
        } else {
            return dateStr;
        }
    }

    public static String convertToShortEnglish(String dateStr) {
        String regex = "(\\d{4})年(\\d{1,2})月";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(dateStr);
        if (matcher.matches()) {
            int year = Integer.parseInt(matcher.group(1));
            int month = Integer.parseInt(matcher.group(2));
            return String.format("%d-%02d", year, month);
        } else {
            return dateStr;
        }
    }

示例运行效果:

Java利用正则表达式实现中英文日期转换函数封装_第1张图片

 

你可能感兴趣的:(Android,Java,java,正则表达式,中英,日期,互转)