• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
左眼看成爱
帮助1

前言: 

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

  1.  
    //带前导零
  2.  
    public static String convertDateToChs(String dateStr) {
  3.  
    String regex = "(\\d{4})-(\\d{1,2})-(\\d{1,2})";
  4.  
    Pattern pattern = Pattern.compile(regex);
  5.  
    Matcher matcher = pattern.matcher(dateStr);
  6.  
    if (matcher.matches()) {
  7.  
    String year = matcher.group(1);
  8.  
    String month = matcher.group(2);
  9.  
    String day = matcher.group(3);
  10.  
    return String.format("%s年%s月%s日", year, month, day);
  11.  
    } else {
  12.  
    return dateStr;
  13.  
    }
  14.  
    }
  15.  
     
  16.  
    //不带前导零
  17.  
    public static String convertToChinese(String dateStr) {
  18.  
    String regex = "(\\d{4})-(\\d{1,2})-(\\d{1,2})";
  19.  
    Pattern pattern = Pattern.compile(regex);
  20.  
    Matcher matcher = pattern.matcher(dateStr);
  21.  
    if (matcher.matches()) {
  22.  
    int year = Integer.parseInt(matcher.group(1));
  23.  
    int month = Integer.parseInt(matcher.group(2));
  24.  
    int day = Integer.parseInt(matcher.group(3));
  25.  
    return String.format("%d年%d月%d日", year, month, day);
  26.  
    } else {
  27.  
    return dateStr;
  28.  
    }
  29.  
    }
  30.  
     
  31.  
    public static String convertDateToEn(String dateStr) {
  32.  
    String regex = "(\\d{4})年(\\d{1,2})月(\\d{1,2})日";
  33.  
    Pattern pattern = Pattern.compile(regex);
  34.  
    Matcher matcher = pattern.matcher(dateStr);
  35.  
    if (matcher.matches()) {
  36.  
    String year = matcher.group(1);
  37.  
    String month = matcher.group(2);
  38.  
    String day = matcher.group(3);
  39.  
    return String.format("%s-%s-%s", year, month, day);
  40.  
    } else {
  41.  
    return dateStr;
  42.  
    }
  43.  
    }
  44.  
     
  45.  
    //使用 String.format() 方法的 d 格式符,将月份和日期格式化为两位数的字符串,自动补齐前导零
  46.  
    public static String convertDateToEnglish(String dateStr) {
  47.  
    String regex = "(\\d{4})年(\\d{1,2})月(\\d{1,2})日";
  48.  
    Pattern pattern = Pattern.compile(regex);
  49.  
    Matcher matcher = pattern.matcher(dateStr);
  50.  
    if (matcher.matches()) {
  51.  
    int year = Integer.parseInt(matcher.group(1));
  52.  
    int month = Integer.parseInt(matcher.group(2));
  53.  
    int day = Integer.parseInt(matcher.group(3));
  54.  
    return String.format("%d-d-d", year, month, day);
  55.  
    } else {
  56.  
    return dateStr;
  57.  
    }
  58.  
    }
  59.  
     
  60.  
    public static String convertToShortChinese(String dateStr) {
  61.  
    String regex = "(\\d{4})-(\\d{1,2})";
  62.  
    Pattern pattern = Pattern.compile(regex);
  63.  
    Matcher matcher = pattern.matcher(dateStr);
  64.  
    if (matcher.matches()) {
  65.  
    int year = Integer.parseInt(matcher.group(1));
  66.  
    int month = Integer.parseInt(matcher.group(2));
  67.  
    return String.format("%d年%d月", year, month);
  68.  
    } else {
  69.  
    return dateStr;
  70.  
    }
  71.  
    }
  72.  
     
  73.  
    public static String convertToShortEnglish(String dateStr) {
  74.  
    String regex = "(\\d{4})年(\\d{1,2})月";
  75.  
    Pattern pattern = Pattern.compile(regex);
  76.  
    Matcher matcher = pattern.matcher(dateStr);
  77.  
    if (matcher.matches()) {
  78.  
    int year = Integer.parseInt(matcher.group(1));
  79.  
    int month = Integer.parseInt(matcher.group(2));
  80.  
    return String.format("%d-d", year, month);
  81.  
    } else {
  82.  
    return dateStr;
  83.  
    }
  84.  
    }

示例运行效果:

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgejahi
系列文章
更多 icon
同类精品
更多 icon
继续加载