在开发程序时,经常会与日期、时长、时间单位等与时间相关的变量、常量、表字段等打交道,但由于英文命名及其缩写的不规范,会造成命名前后不统一,甚至会引成歧义。本文总结出一套比较实用的缩写命名规则,方便以后的研发工作。
与时间相关的概念总的来说包括时间点和时间长度两个。时间点表示事件发生的那一时刻;时长表示事件持续发生多长时间。
时长:duration、timespan、timeslice、interval、period
时间点:timepoint
先来说说时长
时长表示一段时间的长度,时间长度的单位常用及在程序中命名时的英文缩写如下:
中文 | 英文 | 单位的英文缩写 | 说明 |
年 | year | yy | |
月 | month | mo | |
日 | day | dd | |
小时 | hour | hh,h24,h12 | hh默认表示24小时 |
分钟 | minute | mi | |
秒 | second | ss | |
毫秒 | millisecond | ms | |
纳秒 | nanosecond | ns |
举例:
interval_yy | 表示间隔多少年 |
timespan_mo | 表示以月为单位的时间跨度 |
duration_ms | 表示持续的时间长度,以毫秒为单位 |
numPerYy | 表示多少年 |
再来说说时间点
对于时间点,在命名上最好能把类型(日期、时间、日期+时间)和精度(是精确到年、月、日,还是精确到小时、分钟、秒或是毫秒)都能表达出来。
注:为了更好的说明问题,如下表述中使用到的yyyy-MM-dd、yyyy-MM-dd HH:mm:ss,SSS等,只用来表示时间点的精度,不表示日期格式,这样写只是为了在表示时间点精度时看起来方便。
中文 | 英文 | 单位的英文缩写 | 说明 |
日期+时间 | datetime | dtdd | yyyy-MM-dd,精确到日。dt是datetime的缩写。 |
dtmo | yyyy-MM-01,精确到月 | ||
dtyy | yyyy-01-01,精确到年 | ||
dtms | yyyy-MM-dd HH:mm:ss,SSS,dt是datetime的缩写,ms是毫秒的缩写,它们组合在一起:dtms,表示日期+时间,并且精确到毫秒。由于datetime一般都是精确到毫秒,所以也可以直接用dt表示精确到毫秒的 | ||
dtss | yyyy-MM-dd HH:mm:ss,dt是datetime的缩写,s是秒的缩写,它们组合在一起:dts,表示日期+时间,并且精确到秒 | ||
dtmi | yyyy-MM-dd HH:mm:00,dt是datetime的缩写,m是分钟的缩写,它们组合在一起:dtm,表示日期+时间,并且精确到分钟 | ||
dthh | yyyy-MM-dd HH:00:00 | ||
时间 | time | tms | HH:mm:ss。t是time的缩写 |
tmi | HH:mm:00 | ||
thh | HH:00:00 | ||
tss | HH:mm:ss,SSS |
举例:
startDtdd | 表示开始日期,精确到日 |
finishDtmo | 表示完成日期,精确到月 |
start_tss | 表示开始时间,精确到毫秒 |
finish_tmi | 表示完成时间,精确到分钟 |
程序举例一:
public static void doTest01() throws ParseException {
SimpleDateFormat dtfDtms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
SimpleDateFormat dtfDtss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat dtfDtmi = new SimpleDateFormat("yyyy-MM-dd HH:mm");
SimpleDateFormat dtfDthh = new SimpleDateFormat("yyyy-MM-dd HH");
SimpleDateFormat dtfDtdd = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dtfDtmo = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat dtfDtyy = new SimpleDateFormat("yyyy");
SimpleDateFormat dtfTms = new SimpleDateFormat("HH:mm:ss,SSS");
SimpleDateFormat dtfTss = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat dtfTmi = new SimpleDateFormat("HH:mm");
SimpleDateFormat dtfThh = new SimpleDateFormat("HH");
Date startDateTimeDtms = new Date();
System.out.println(dtfDtms.format(startDateTimeDtms));
System.out.println(dtfDtss.format(startDateTimeDtms));
System.out.println(dtfDtmi.format(startDateTimeDtms));
System.out.println(dtfDthh.format(startDateTimeDtms));
System.out.println(dtfDtdd.format(startDateTimeDtms));
System.out.println(dtfDtmo.format(startDateTimeDtms));
System.out.println(dtfDtyy.format(startDateTimeDtms));
System.out.println(dtfDtdd.format(startDateTimeDtms));
System.out.println(dtfDtmo.format(startDateTimeDtms));
System.out.println(dtfDtyy.format(startDateTimeDtms));
System.out.println(dtfTms.format(startDateTimeDtms));
System.out.println(dtfTss.format(startDateTimeDtms));
System.out.println(dtfTmi.format(startDateTimeDtms));
System.out.println(dtfThh.format(startDateTimeDtms));
System.out.println("------------------------------------------------");
String startDateTimeStrDtmi = dtfDtmi.format(startDateTimeDtms);
System.out.println(startDateTimeStrDtmi);
Date startDateTimeDtmi = dtfDtmi.parse(startDateTimeStrDtmi);
System.out.println(dtfDtms.format(startDateTimeDtmi));
System.out.println("------------------------------------------------");
String startDateTimeStrDthh = dtfDthh.format(startDateTimeDtms);
System.out.println(startDateTimeStrDthh);
Date startDateTimeDthh = dtfDthh.parse(startDateTimeStrDthh);
System.out.println(dtfDtms.format(startDateTimeDthh));
System.out.println("------------------------------------------------");
String startDateTimeStrDtdd = dtfDtdd.format(startDateTimeDtms);
System.out.println(startDateTimeStrDtdd);
Date startDateDtdd = dtfDtdd.parse(startDateTimeStrDtdd);
System.out.println(dtfDtms.format(startDateDtdd));
System.out.println("------------------------------------------------");
String startDateTimeStrDtmo = dtfDtmo.format(startDateTimeDtms);
System.out.println(startDateTimeStrDtmo);
Date startDateDtmo = dtfDtmo.parse(startDateTimeStrDtmo);
System.out.println(dtfDtms.format(startDateDtmo));
System.out.println("------------------------------------------------");
String startDateTimeStrDtyy = dtfDtyy.format(startDateTimeDtms);
System.out.println(startDateTimeStrDtyy);
Date startDateDtyy = dtfDtyy.parse(startDateTimeStrDtyy);
System.out.println(dtfDtms.format(startDateDtyy));
}
运行结果:
2019-11-13 00:20:02,176
2019-11-13 00:20:02
2019-11-13 00:20
2019-11-13 00
2019-11-13
2019-11
2019
2019-11-13
2019-11
2019
00:20:02,176
00:20:02
00:20
00
------------------------------------------------
2019-11-13 00:20
2019-11-13 00:20:00,000
------------------------------------------------
2019-11-13 00
2019-11-13 00:00:00,000
------------------------------------------------
2019-11-13
2019-11-13 00:00:00,000
------------------------------------------------
2019-11
2019-11-01 00:00:00,000
------------------------------------------------
2019
2019-01-01 00:00:00,000
程序举例二:
public static void doTest02() {
DateTimeFormatter dtfDtms = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS");
DateTimeFormatter dtfDtss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter dtfDtmi = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
DateTimeFormatter dtfDthh = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
DateTimeFormatter dtfDtdd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dtfDtmo = DateTimeFormatter.ofPattern("yyyy-MM");
DateTimeFormatter dtfDtyy = DateTimeFormatter.ofPattern("yyyy");
DateTimeFormatter dtfTms = DateTimeFormatter.ofPattern("HH:mm:ss,SSS");
DateTimeFormatter dtfTss = DateTimeFormatter.ofPattern("HH:mm:ss");
DateTimeFormatter dtfTmi = DateTimeFormatter.ofPattern("HH:mm");
DateTimeFormatter dtfThh = DateTimeFormatter.ofPattern("HH");
LocalDateTime startDateTimeDtms = LocalDateTime.now();
System.out.println(dtfDtms.format(startDateTimeDtms));
System.out.println(dtfDtss.format(startDateTimeDtms));
System.out.println(dtfDtmi.format(startDateTimeDtms));
System.out.println(dtfDthh.format(startDateTimeDtms));
System.out.println(dtfDtdd.format(startDateTimeDtms));
System.out.println(dtfDtmo.format(startDateTimeDtms));
System.out.println(dtfDtyy.format(startDateTimeDtms));
LocalDate startDateDthh = LocalDate.now();
System.out.println(dtfDtdd.format(startDateDthh));
System.out.println(dtfDtmo.format(startDateDthh));
System.out.println(dtfDtyy.format(startDateDthh));
LocalTime startTimeTms = LocalTime.now();
System.out.println(dtfTms.format(startTimeTms));
System.out.println(dtfTss.format(startTimeTms));
System.out.println(dtfTmi.format(startTimeTms));
System.out.println(dtfThh.format(startTimeTms));
System.out.println("------------------------------------------------");
String startDateTimeStrDtmi = dtfDtmi.format(startDateTimeDtms);
System.out.println(startDateTimeStrDtmi);
LocalDateTime startDateTimeDtmi = LocalDateTime.parse(startDateTimeStrDtmi, dtfDtmi);
System.out.println(dtfDtms.format(startDateTimeDtmi));
System.out.println("------------------------------------------------");
String startDateTimeStrDthh = dtfDthh.format(startDateTimeDtms);
System.out.println(startDateTimeStrDthh);
LocalDateTime startDateTimeDthh = LocalDateTime.parse(startDateTimeStrDthh, dtfDthh);
System.out.println(dtfDtms.format(startDateTimeDthh));
System.out.println("------------------------------------------------");
String startDateTimeStrDtdd = dtfDtdd.format(startDateTimeDtms);
System.out.println(startDateTimeStrDtdd);
LocalDate startDateDtdd = LocalDate.parse(startDateTimeStrDtdd, dtfDtdd);
System.out.println(dtfDtms.format(LocalDateTime.of(startDateDtdd, LocalTime.of(0, 0, 0, 0))));
System.out.println("------------------------------------------------");
String startDateTimeStrDtmo = dtfDtmo.format(startDateTimeDtms);
System.out.println(startDateTimeStrDtmo);
LocalDate startDateDtmo = LocalDate.parse(startDateTimeStrDtmo + "-01", dtfDtdd);
System.out.println(dtfDtms.format(LocalDateTime.of(startDateDtmo, LocalTime.of(0, 0, 0, 0))));
System.out.println("------------------------------------------------");
String startDateTimeStrDtyy = dtfDtyy.format(startDateTimeDtms);
System.out.println(startDateTimeStrDtyy);
LocalDate startDateDtyy = LocalDate.parse(startDateTimeStrDtyy + "-01-01", dtfDtdd);
System.out.println(dtfDtms.format(LocalDateTime.of(startDateDtyy, LocalTime.of(0, 0, 0, 0))));
}
运行结果:
2019-11-15 16:03:25,904
2019-11-15 16:03:25
2019-11-15 16:03
2019-11-15 16
2019-11-15
2019-11
2019
2019-11-15
2019-11
2019
16:03:25,904
16:03:25
16:03
16
------------------------------------------------
2019-11-15 16:03
2019-11-15 16:03:00,000
------------------------------------------------
2019-11-15 16
2019-11-15 16:00:00,000
------------------------------------------------
2019-11-15
2019-11-15 00:00:00,000
------------------------------------------------
2019-11
2019-11-01 00:00:00,000
------------------------------------------------
2019
2019-01-01 00:00:00,000