Java - Java样板代码集合

Java样板代码集合

Date与LocalDate互转

Java8 日期时间API,新增了LocalDate、LocalDateTime、LocalTime等线程安全类:

  • LocalDate:只有日期,诸如:2019-07-13
  • LocalTime:只有时间,诸如:08:30
  • LocalDateTime:日期+时间,诸如:2019-07-13 08:30

Date转换成LocalDate

public static LocalDate date2LocalDate(Date date) {
        if(null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}

LocalDate转换成Date

 public static Date localDate2Date(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
}

LocalDateTime转换成Date

public static Date localDateTime2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}

LocalDate格式化

public static String formatDate(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}

Java 模板变量替换(字符串、占位符替换)

org.apache.commons.text

变量默认前缀是 ${ ,后缀是 } 

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.12</version>
</dependency>
Map valuesMap = new HashMap();
valuesMap.put("code", 1234);
String templateString = "验证码:${code},您正在登录管理后台,5分钟内输入有效。";
StringSubstitutor sub = new StringSubstitutor(valuesMap);
String content= sub.replace(templateString);
System.out.println(content);

修改占位符前/后缀

Map valuesMap = new HashMap();
valuesMap.put("code", 1234);
String templateString = "验证码:[code],您正在登录管理后台,5分钟内输入有效。";
StringSubstitutor sub = new StringSubstitutor(valuesMap);
//修改前缀、后缀
sub.setVariablePrefix("[");
sub.setVariableSuffix("]");
String content= sub.replace(templateString);
System.out.println(content);

org.springframework.expression

String smsTemplate = "验证码:#{[code]},您正在登录管理后台,5分钟内输入有效。";
Map<String, Object> params = new HashMap<>();
params.put("code", 12345);;
 
ExpressionParser parser = new SpelExpressionParser();
TemplateParserContext parserContext = new TemplateParserContext();
String content = parser.parseExpression(smsTemplate,parserContext).getValue(params, String.class);
 
System.out.println(content);

ExpressionParser是简单的用java编写的表达式解析器,官方文档:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html

java.text.MessageFormat

Object[] params = new Object[]{"1234", "5"};
String msg = MessageFormat.format("验证码:{0},您正在登录管理后台,{1}分钟内输入有效。", params);
System.out.println(msg);

java.lang.String

String s = String.format("My name is %s. I am %d.", "Tom", 18);
System.out.println(s);
转换符详细说明示例
%s 字符串类型 “string”
%c 字符类型 'm'
%b 布尔类型 true
%d 整数类型(十进制) 88
%x 整数类型(十六进制) FF
%o 整数类型(八进制) 77
%f 浮点类型 8.888
%a 十六进制浮点类型 FF.35AE
%e 指数类型 9.38e+5
%g 通用浮点类型(f和e类型中较短的) /
%h 散列码 /
%% 百分比类型 %(%特殊字符%%才能显示%)
%n 换行符 /
%tx 日期与时间类型(x代表不同的日期与时间转换符) /

posted @ 2022-12-15 17:22  Helios_Fz  阅读(68)  评论(0编辑  收藏  举报