Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String... strings) {
- // Old
- final String old1 = DateFormat.format_yyyyMMdd(new Date());
- final String old2 = DateFormat.format_yyyy_MM_dd(new Date());
- // New
- final String new1 = format(LocalDate.now(), YYMMDD_PATTERN);
- final String new2 = format(LocalDate.now(), YY_MM_DD_PATTERN);
- System.out.println(Objects.equals(old1, new1));
- System.out.println(Objects.equals(old2, new2));
- long currentMs = System.currentTimeMillis();
- DateFormat.format(currentMs);
- }
- /**************
- * OLD SOURCE CLONE
- **************
- */
- public static final int JavaDate_StartYear = 1900;
- private static final String Zero = "0";
- private static String format2DNumber(int n) {
- return n > 9 ? String.valueOf(n) : (Zero + n);
- }
- public static String format_yyyyMMdd(Date d) {
- if (d == null) {
- return null;
- }
- StringBuilder buidler = new StringBuilder()
- .append(d.getYear() + JavaDate_StartYear)
- .append(format2DNumber(d.getMonth() + 1))
- .append(format2DNumber(d.getDate()));
- // .append( getMillisecond( d ) );
- return buidler.toString();
- }
- public static String format_yyyy_MM_dd(Date d) {
- if (d == null) {
- return null;
- }
- StringBuilder buidler = new StringBuilder()
- .append(d.getYear() + JavaDate_StartYear)
- .append("_")
- .append(format2DNumber(d.getMonth() + 1))
- .append("_")
- .append(format2DNumber(d.getDate()));
- // .append( getMillisecond( d ) );
- return buidler.toString();
- }
- // End
- /**************
- * NEW SOURCE CLONE
- **************
- */
- private static final DateTimeFormatter YYMMDD_PATTERN = DateTimeFormatter.ofPattern("yyyyMMdd");
- private static final DateTimeFormatter YY_MM_DD_PATTERN = DateTimeFormatter.ofPattern("yyyy_MM_dd");
- public static final String format(TemporalAccessor temporal, DateTimeFormatter formatter) {
- return formatter.format(temporal);
- }
- // End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement