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
和 d
格式符将年份和月份格式化为不带前导零和带前导零的字符串。该代码首先使用正则表达式将输入的日期字符串匹配出年、月、日三个部分,然后将月份和日期字符串使用 Integer.parseInt()
方法转换为整数,最后使用 d
格式符将月份和日期格式化为两位数的字符串,自动补齐前导零。如果转为中文日期则去掉前导零。
-
//带前导零
-
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() 方法的 d 格式符,将月份和日期格式化为两位数的字符串,自动补齐前导零
-
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-d-d", 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-d", year, month);
-
} else {
-
return dateStr;
-
}
-
}
示例运行效果:
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgejahi
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01