@Test public void testFormat() { String format = "dt=#YYYYMMdd#"; Long time = 1516186620155L; String timeZone = "GMT+8:00"; assertEquals(TimeUtil.format(format, time, TimeZone .getTimeZone(timeZone)), "dt=20180117"); }
public static String format(String timeFormat, long time, TimeZone timeZone) { String timePattern = "#(?:\\\\#|[^#])*#"; Date t = new Date(time); Pattern ptn = Pattern.compile(timePattern); Matcher matcher = ptn.matcher(timeFormat); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String group = matcher.group(); String content = group.substring(1, group.length() - 1); String pattern = refreshEscapeHashTag(content); SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(timeZone); matcher.appendReplacement(sb, sdf.format(t)); } matcher.appendTail(sb); return refreshEscapeHashTag(sb.toString()); }
TimeUtil { public static String format(String timeFormat, long time, TimeZone timeZone) { String timePattern = "#(?:\\\\#|[^#])*#"; Date t = new Date(time); Pattern ptn = Pattern.compile(timePattern); Matcher matcher = ptn.matcher(timeFormat); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String group = matcher.group(); String content = group.substring(1, group.length() - 1); String pattern = refreshEscapeHashTag(content); SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(timeZone); matcher.appendReplacement(sb, sdf.format(t)); } matcher.appendTail(sb); return refreshEscapeHashTag(sb.toString()); } }
TimeUtil { public static String format(String timeFormat, long time, TimeZone timeZone) { String timePattern = "#(?:\\\\#|[^#])*#"; Date t = new Date(time); Pattern ptn = Pattern.compile(timePattern); Matcher matcher = ptn.matcher(timeFormat); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String group = matcher.group(); String content = group.substring(1, group.length() - 1); String pattern = refreshEscapeHashTag(content); SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(timeZone); matcher.appendReplacement(sb, sdf.format(t)); } matcher.appendTail(sb); return refreshEscapeHashTag(sb.toString()); }  }
TimeUtil { public static String format(String timeFormat, long time, TimeZone timeZone) { String timePattern = "#(?:\\\\#|[^#])*#"; Date t = new Date(time); Pattern ptn = Pattern.compile(timePattern); Matcher matcher = ptn.matcher(timeFormat); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String group = matcher.group(); String content = group.substring(1, group.length() - 1); String pattern = refreshEscapeHashTag(content); SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(timeZone); matcher.appendReplacement(sb, sdf.format(t)); } matcher.appendTail(sb); return refreshEscapeHashTag(sb.toString()); }  static Long str2Long(String timeStr); static String format(String timeFormat, long time, TimeZone timeZone); static TimeZone getTimeZone(String timezone); }
TimeUtil { public static String format(String timeFormat, long time, TimeZone timeZone) { String timePattern = "#(?:\\\\#|[^#])*#"; Date t = new Date(time); Pattern ptn = Pattern.compile(timePattern); Matcher matcher = ptn.matcher(timeFormat); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String group = matcher.group(); String content = group.substring(1, group.length() - 1); String pattern = refreshEscapeHashTag(content); SimpleDateFormat sdf = new SimpleDateFormat(pattern); sdf.setTimeZone(timeZone); matcher.appendReplacement(sb, sdf.format(t)); } matcher.appendTail(sb); return refreshEscapeHashTag(sb.toString()); }  static Long str2Long(String timeStr); static String format(String timeFormat, long time, TimeZone timeZone); static TimeZone getTimeZone(String timezone);  }