1919package org .apache .hadoop .yarn .util ;
2020
2121import java .text .ParseException ;
22- import java .text .SimpleDateFormat ;
23- import java .util .Date ;
22+ import java .time .Instant ;
23+ import java .time .ZoneId ;
24+ import java .time .format .DateTimeFormatter ;
2425
2526import org .slf4j .Logger ;
2627import org .slf4j .LoggerFactory ;
@@ -31,23 +32,16 @@ public class Times {
3132 private static final Logger LOG =
3233 LoggerFactory .getLogger (Times .class );
3334
34- static final String ISO8601DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ;
35+ static final String ISO8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ;
3536
3637 // This format should match the one used in yarn.dt.plugins.js
37- static final ThreadLocal <SimpleDateFormat > dateFormat =
38- new ThreadLocal <SimpleDateFormat >() {
39- @ Override protected SimpleDateFormat initialValue () {
40- return new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy" );
41- }
42- };
38+ static final DateTimeFormatter DATE_FORMAT =
39+ DateTimeFormatter .ofPattern ("EEE MMM dd HH:mm:ss Z yyyy" ).withZone (
40+ ZoneId .systemDefault ());
4341
44- static final ThreadLocal <SimpleDateFormat > isoFormat =
45- new ThreadLocal <SimpleDateFormat >() {
46- @ Override
47- protected SimpleDateFormat initialValue () {
48- return new SimpleDateFormat (ISO8601DATEFORMAT );
49- }
50- };
42+ static final DateTimeFormatter ISO_OFFSET_DATE_TIME =
43+ DateTimeFormatter .ofPattern (ISO8601_DATE_FORMAT ).withZone (
44+ ZoneId .systemDefault ());
5145
5246 public static long elapsed (long started , long finished ) {
5347 return Times .elapsed (started , finished , true );
@@ -83,8 +77,7 @@ public static long elapsed(long started, long finished, boolean isRunning) {
8377 }
8478
8579 public static String format (long ts ) {
86- return ts > 0 ? String .valueOf (dateFormat .get ().format (new Date (ts )))
87- : "N/A" ;
80+ return ts > 0 ? DATE_FORMAT .format (Instant .ofEpochMilli (ts )) : "N/A" ;
8881 }
8982
9083 /**
@@ -94,7 +87,7 @@ public static String format(long ts) {
9487 * @return ISO 8601 formatted string.
9588 */
9689 public static String formatISO8601 (long ts ) {
97- return isoFormat . get (). format (new Date (ts ));
90+ return ISO_OFFSET_DATE_TIME . format (Instant . ofEpochMilli (ts ));
9891 }
9992
10093 /**
@@ -109,6 +102,6 @@ public static long parseISO8601ToLocalTimeInMillis(String isoString)
109102 if (isoString == null ) {
110103 throw new ParseException ("Invalid input." , -1 );
111104 }
112- return isoFormat . get () .parse (isoString ). getTime ();
105+ return Instant . from ( ISO_OFFSET_DATE_TIME .parse (isoString )). toEpochMilli ();
113106 }
114107}
0 commit comments