Java uses regular expressions to realize the encapsulation of Chinese and English date conversion functions

Foreword: 

Java uses regular expressions to realize the encapsulation of Chinese and English date conversion functions, and implements a total of 6 methods (the reason for the encapsulation is that it is too troublesome to use SimpleDateFormat in Android, and various try catch exceptions are particularly troublesome, but if you use JDK8's LocalDate, It loses support for lower version OS (jdk8 needs at least Android 8.0+ to support) and is unwilling to use third-party date packages, so...)

The following is a sample code for converting "yyyy-MM-dd" to "Yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy M to M month d day" or "yyyy-MM" to "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy-m-dd-MM-MM-MM” code conversions-MM-MM” has achieved mutual conversion, simple and easy to use.

Use  the %d ampersand  %02d format specifier to format the year and month as a string without and with leading zeros. The code first uses regular expressions to match the input date string to three parts of year, month, and day, then converts the month and date strings to integers using the method, and finally uses the format  Integer.parseInt() character  %02d to format the month and date into two digits A string of numbers, automatically padded with leading zeros. If it is converted to a Chinese date, remove the leading zero. 

//带前导零    
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;
        }
    }

Example running effect:

 

Guess you like

Origin blog.csdn.net/wh445306/article/details/130292895