Blankj/AndroidUtilCode · error · UnsupportedOperationException

u can't instantiate me...

Error message

u can't instantiate me...

What it means

TimeUtils is a final static-only utility for date/time formatting and conversion, backed by a thread-local SimpleDateFormat cache. Its private constructor throws UnsupportedOperationException to prevent instantiation. All public methods (millis2String, getSafeDateFormat, etc.) are static.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/TimeUtils.java:69

            return android.provider.Settings.System.getInt(Utils.getApp().getContentResolver(), android.provider.Settings.System.AUTO_TIME, 0) == 1;
        }
    }
    

    @SuppressLint("SimpleDateFormat")
    public static SimpleDateFormat getSafeDateFormat(String pattern) {
        Map<String, SimpleDateFormat> sdfMap = SDF_THREAD_LOCAL.get();
        //noinspection ConstantConditions
        SimpleDateFormat simpleDateFormat = sdfMap.get(pattern);
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat(pattern);
            sdfMap.put(pattern, simpleDateFormat);
        }
        return simpleDateFormat;
    }

    private TimeUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    /**
     * Milliseconds to the formatted time string.
     * <p>The pattern is {@code yyyy-MM-dd HH:mm:ss}.</p>
     *
     * @param millis The milliseconds.
     * @return the formatted time string
     */
    public static String millis2String(final long millis) {
        return millis2String(millis, getDefaultFormat());
    }

    /**
     * Milliseconds to the formatted time string.
     *
     * @param millis  The milliseconds.
     * @param pattern The pattern of date format, such as yyyy/MM/dd HH:mm

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Use static methods: TimeUtils.millis2String(millis) or TimeUtils.getSafeDateFormat(pattern) instead of constructing.
  2. Provide a Gson InstanceCreator / Jackson mixin returning a sentinel to avoid constructor invocation.
  3. Mock static methods with Mockito.mockStatic rather than constructing TimeUtils.
  4. Keep the class final with a private constructor.

Example fix

// before
TimeUtils tu = new TimeUtils();

// after
String s = TimeUtils.millis2String(System.currentTimeMillis());
Defensive patterns

Strategy: validation

Validate before calling

if (Modifier.isFinal(TimeUtils.class.getModifiers())) {
  throw new IllegalStateException("TimeUtils is static-only.");
}

Prevention

When it happens

Trigger: Reflective instantiation via TimeUtils.class.getDeclaredConstructor().setAccessible(true).newInstance(), or transitively via Gson/Jackson deserialization, Mockito/PowerMock, Kotlin reflection, or a DI classpath scan.

Common situations: JSON mapping a field to TimeUtils; test frameworks mocking date utilities; coverage probes; accidental construction after weakening access modifiers.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/d9f07928cec6b076. Report an issue: GitHub.