2021SC@SDUSC
上篇文章将分析src/main下的process文件夹下的类。总共三个类,分别是ProcessResult类、ProcessHelper类和ProcessResultList类,均已分析完毕。接下来文章将继续对main文件下的类进行分析。
目录
ws
首先先看ws文件下的类:该文件下只有一个IClusterWsMessage接口,该接口继承了Serializable接口,实例类实现Serializable接口 目的是为了将其序列化。序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
public interface IClusterWsMessage extends Serializable {
}
Serializable接口其实是一个空接口,里面没有任何方法的实现,是一个标识性的接口,用来通知jvm对这个类做序列化处理。
CalendarHelper类
public class CalendarHelper {
public static ZoneId getZoneId(String tzId) {
return ZoneId.of(tzId, ZoneId.SHORT_IDS);
}
public static Date getDate(LocalDate d, String tzId) {
return getDate(d.atStartOfDay(), tzId);
}
public static Date getDate(LocalDateTime d, String tzId) {
return new Date(d.atZone(getZoneId(tzId)).toInstant().toEpochMilli());
}
public static ZonedDateTime getZoneDateTime(Date d, String tzId) {
long milli = (d == null ? new Date() : d).getTime();
return Instant.ofEpochMilli(milli).atZone(getZoneId(tzId));
}
public static LocalDate getDate(Date d, String tzId) {
return getZoneDateTime(d == null ? new Date() : d, tzId).toLocalDate();
}
public static LocalDateTime getDateTime(Date d, String tzId) {
return getZoneDateTime(d == null ? new Date() : d, tzId).toLocalDateTime();
}
public static String formatMillis(long millis) {
long m = millis;
long hours = TimeUnit.MILLISECONDS.toHours(m);
m -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(m);
m -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(m);
m -= TimeUnit.SECONDS.toMillis(seconds);
return String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, m);
}
}
该CalendarHelper类定义了几个静态方法getZoneId()、getDate()、getZoneDateTime()、getDateTime()、getDate()、getDateTime()来分别获取系统时区、获取日期、获取本地日期和时间、获取本地日期、获取本地时间。根据所需来调用不同的方法获取相应的时间类型。
然后定义了静态方法String formatMillis(),其中TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段,里面的方法有:
public long toMillis(long d) //转化成毫秒
public long toSeconds(long d) //转化成秒
public long toMinutes(long d) //转化成分钟
public long toHours(long d) //转化成小时
public long toDays(long d) //转化天
即传入的参数millis转化为对应的时间:小时:分钟:秒的格式。
CalendarPatterns类
public class CalendarPatterns {
private static final Logger log = Red5LoggerFactory.getLogger(CalendarPatterns.class, getWebAppRootKey());
public static final FastDateFormat dateFormat__ddMMyyyyHHmmss = FastDateFormat.getInstance("dd.MM.yyyy HH:mm:ss");
public static final FastDateFormat dateFormat__ddMMyyyy = FastDateFormat.getInstance("dd.MM.yyyy");
public static final FastDateFormat dateFormat__ddMMyyyyBySeparator = FastDateFormat.getInstance("dd-MM-yyyy");
public static final FastDateFormat dateFormat__yyyyMMddHHmmss = FastDateFormat.getInstance("yyyy.MM.dd HH:mm:ss");
public static final FastDateFormat STREAM_DATE_FORMAT = FastDateFormat.getInstance("yyyy_MM_dd_HH_mm_ss");
public static final String FULL_DF_PATTERN = "dd.MM.yyyy HH:mm:ss z (Z)";
public static final String ISO8601_FULL_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ssZ";
public static final String ISO8601_DATETIME_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ss";
public static final String ISO8601_DATE_FORMAT_STRING = "yyyy-MM-dd";
public static final FastDateFormat FULL_DATE_FORMAT = FastDateFormat.getInstance(FULL_DF_PATTERN);
public static final FastDateFormat ISO8601_FULL_FORMAT = FastDateFormat.getInstance(ISO8601_FULL_FORMAT_STRING);
public static final FastDateFormat ISO8601_DATETIME_FORMAT = FastDateFormat.getInstance(ISO8601_DATETIME_FORMAT_STRING);
public static final FastDateFormat ISO8601_DATE_FORMAT = FastDateFormat.getInstance(ISO8601_DATE_FORMAT_STRING);
private CalendarPatterns() {}
public static String getDateByMiliSeconds(Date t) {
return dateFormat__yyyyMMddHHmmss.format(t);
}
public static String getDateWithTimeByMiliSeconds(Date t) {
return t == null ? null : dateFormat__yyyyMMddHHmmss.format(t);
}
public static String getDateWithTimeByMiliSecondsWithZone(Date t) {
return t == null ? null : FULL_DATE_FORMAT.format(t);
}
public static String getExportDate(Date t) {
if (t == null) {
return "";
}
return String.valueOf(t.getTime());
}
public static Date parseImportDate(String dateString) {
try {
Date resultDate = validDate(dateFormat__ddMMyyyyHHmmss, dateString);
if (resultDate != null) {
return resultDate;
}
resultDate = validDate(dateFormat__ddMMyyyy, dateString);
if (resultDate != null) {
return resultDate;
}
resultDate = validDate(dateString);
if (dateString != null) {
return resultDate;
}
log.error("parseDate:: Could not parse date string {}", dateString);
} catch (Exception e) {
log.error("parseDate", e);
}
return null;
}
private static Date validDate(String testdate) {
try {
Long t = Long.valueOf(testdate);
if (t != null) {
return new Date(t);
}
} catch (Exception err) {
//no-op
}
return null;
}
private static Date validDate(FastDateFormat sdf, String testdate) {
Date resultDate = null;
try {
resultDate = sdf.parse(testdate);
} catch (ParseException|NumberFormatException e) {
// if the format of the string provided doesn't match the format we
// declared in SimpleDateFormat() we will get an exception
return null;
}
if (!sdf.format(resultDate).equals(testdate)) {
return null;
}
return resultDate;
}
public static String getDateWithTimeByMiliSecondsAndTimeZone(Date t, TimeZone timezone) {
return t == null ? null : FastDateFormat.getInstance(FULL_DF_PATTERN, timezone).format(t);
}
public static String getTimeForStreamId(Date t) {
return STREAM_DATE_FORMAT.format(t);
}
public static Date parseDate(String dateString) {
try {
return dateFormat__ddMMyyyy.parse(dateString);
} catch (Exception e) {
log.error("parseDate", e);
}
return null;
}
public static Date parseDateBySeparator(String dateString) {
try {
return dateFormat__ddMMyyyyBySeparator.parse(dateString);
} catch (Exception e) {
log.error("parseDate", e);
}
return null;
}
public static Date parseDateWithHour(String dateString) {
try {
if (dateString == null || dateString.length() == 0
|| "null".equals(dateString)) {
return null;
}
return dateFormat__ddMMyyyyHHmmss.parse(dateString);
} catch (Exception e) {
log.error("parseDate", e);
}
return null;
}
}
在CalendarPatterns类中,首先定义了一个日志对象log来打印日志。然后定义了一系列FastDateFormat类对象来为时间定义不同的格式比如dd.MM.yyyy HH:mm:ss、dd.MM.yyyy、dd-MM-yyyy等格式,定义了字符串类型的常量也为日期时间等设置格式。
设置了一个空构造方法,然后定义了三个get方法来分别获取三种格式的日期时间形式,getExportDate()方法将日期形式转化为字符串,然后parseImportDate()方法是用来解析日期字符串返回相应格式的日期形式。然后validDate()方法则是用来获取有效日期时间对象,如果字符串的格式与我们提供的不匹配,在SimpleDateFormat()中声明,我们将得到一个异常。
然后getDateWithTimeByMiliSecondsAndTimeZone()方法以毫秒和时区获取日期和时间,getTimeForStreamId()方法返回流日期格式,parseDate()方法即返回ddMMyyyy格式的日期,parseDateBySeparator()方法返回dd-MM—yyyy的日期格式的日期、parseDateWithHour()方法返回yyyy.MM.dd HH:mm:ss的日期格式的日期。
ImportHelper类
public class ImportHelper {
private ImportHelper() {}
public static Map<String, String> getAllTimeZones(String ... tzList) {
Map<String, String> result = new LinkedHashMap<>();
for (String omTimeZone : tzList) {
result.put(omTimeZone, omTimeZone);
}
return result;
}
}
ImportHelper类用来获取一些上传的默认值。
其中只有一个静态方法Map<String, String> getAllTimeZones(String ... tzList),以Map对象的形式返回所有时区的列表,把参数tzList放到result结果对象里面然后返回一个map对象以经过的时区作为key和value。
总结
本篇文章继续对main下的类进行了分析,对ws下的IClusterWsMessage接口,main下的CalendarHelper类、CalendarPatterns类、ImportHelper类进行了分析。往后将继续分析main下的类。