今天使用SimpleDateFormat重置pattern的时候发现有两个方法一个applyPattern,另一个是applyLocalizedPattern遂好奇这两个方法有何不同。
测试代码(因第一次报错发现yyyy对应的是aaaa,所以applyLocalizedPatter改成aaaa)
package com.hh;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
* @author hh
* @date 2019/7/1
*/
public class Test {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-hh-ss");
System.out.println("toPattern: " + df.toPattern());
System.out.println("toLocalizedPattern:" + df.toLocalizedPattern());
System.out.println();
df.applyPattern("yyyyhhss");
System.out.println("toPattern: " + df.toPattern());
System.out.println("toLocalizedPattern:" + df.toLocalizedPattern());
System.out.println();
df.applyLocalizedPattern("aaaa-hh-ss");
System.out.println("toPattern: " + df.toPattern());
System.out.println("toLocalizedPattern:" + df.toLocalizedPattern());
System.out.println();
df.applyLocalizedPattern("aaaahhss");
System.out.println("toPattern: " + df.toPattern());
System.out.println("toLocalizedPattern:" + df.toLocalizedPattern());
System.out.println();
}
}
运行结果:
猜测
是不是两种关系式有某种关联部分会有区别
然后进行查看源码并debug发现两种apply方法中就只有一个方法不同
debug进入translatePattern()方法发现里面是一个类似转换的功能,将对应的某个字符转换成另一个字符
随后好奇formatData.getLocalPatternChars()为什么是“GanjkHmsSEDFwWxhKzZ”
发现初始化localPatternChars的时候使用的是zh-CN,这个咱知道,难倒是中国语言环境中解释日期和时间模式匹配字段?
可是为什么中国语言环境中年是用的aaaa,这个现在不知,等后面知道了再补充一下。