Skip to content

FormatUtil

Utility class with static helper methods for formatting sizes, dates, time intervals, and strings; sanitizing and escaping HTML; and validating common data types. All methods are static.

Description:

Method Return values Description
formatSize(long bytes) String Formats a byte count as a human-readable size string (e.g. 1.5 MB). Units: B, KB, MB, GB, TB, PB, EB.
  • bytes: The size in bytes.

Example:

package com.openkm;
import java.io.File;
import com.openkm.util.FormatUtil;
public class Test {
public static void main(String[] args) {
try {
File file = new File("/home/openkm/test.png");
System.out.println(FormatUtil.formatSize(file.length())); // e.g. "2.3 MB"
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
parseSize(String text) long Parses a human-readable size string (e.g. "10MB", "2GB") into bytes. Supports KB, MB, GB suffixes.
  • text: The size string to parse.

Example:

long bytes = FormatUtil.parseSize("10MB");
System.out.println(bytes); // 10485760

Description:

Method Return values Description
formatDate(Calendar cal) String Formats a Calendar value as dd-MM-yyyy HH:mm:ss in the calendar’s own time zone. Returns null if cal is null.

Example:

import java.util.Calendar;
import com.openkm.util.FormatUtil;
System.out.println(FormatUtil.formatDate(Calendar.getInstance())); // "15-03-2024 14:30:00"

Description:

Method Return values Description
formatDate(Date date) String Formats a Date value as dd-MM-yyyy HH:mm:ss.

Description:

Method Return values Description
formatDate(long unixEpoch) String Formats a Unix epoch value (milliseconds since 1970-01-01) as dd-MM-yyyy HH:mm:ss.

Example:

System.out.println(FormatUtil.formatDate(System.currentTimeMillis())); // "15-03-2024 14:30:00"

Description:

Method Return values Description
formatSeconds(long time) String Formats a duration in milliseconds as HH:MM:SS.

Description:

Method Return values Description
formatSecondsSince(long time) String Formats the elapsed time since the given start timestamp (in milliseconds) as HH:MM:SS. Pass the result of System.currentTimeMillis() at the start of the operation.

Description:

Method Return values Description
formatMilliSeconds(long time) String Formats a duration in milliseconds as HH:MM:SS.mmm.

Description:

Method Return values Description
formatMilliSecondsSince(long time) String Formats the elapsed time since the given start timestamp (in milliseconds) as HH:MM:SS.mmm. Pass the result of System.currentTimeMillis() at the start of the operation.

Example:

long start = System.currentTimeMillis();
// ... long operation ...
System.out.println("Elapsed: " + FormatUtil.formatMilliSecondsSince(start)); // "00:00:01.234"

Description:

Method Return values Description
formatArray(String[] values) String Converts a string array to a readable string. Returns the single element directly for one-element arrays, and the Apache Commons ArrayUtils.toString representation for multi-element arrays. Returns null for null input.

Description:

Method Return values Description
formatObject(Object value) String Converts an object to a string. For Object[] uses ArrayUtils.toString; otherwise calls toString(). Returns null for null input.

Description:

Method Return values Description
escapeHtml(String str) String Escapes < and > as HTML entities, and converts newlines to <br/>. Returns null for null input.

Description:

Method Return values Description
sanitizeInput(String string) String Sanitizes an HTML string to remove potentially dangerous content (XSS). Uses either the AntiSamy library or a legacy regex approach depending on the Config.SECURITY_INPUT_SANITIZER setting. Returns null for null input.

Description:

Method Return values Description
cleanXSS(String value) String Cleans a string by escaping HTML angle brackets and parentheses, removing eval() expressions and inline JavaScript, and stripping script keywords.

Description:

Method Return values Description
sanitizeEmailString(String value) String Removes any characters that are not letters, digits, punctuation, or spaces from the given string. Useful for sanitizing email address strings.

Example:

String safe = FormatUtil.escapeHtml("<b>Hello</b>\nWorld");
// "&lt;b&gt;Hello&lt;/b&gt;<br/>World"
String sanitized = FormatUtil.sanitizeInput("<script>alert('xss')</script>Hello");
System.out.println(sanitized); // "Hello"

Description:

Method Return values Description
isValidUUID(String str) boolean Returns true if the given string is a valid UUID.

Example:

boolean valid = FormatUtil.isValidUUID("d50133e3-dbfa-4d01-a109-28785cd48f40"); // true
boolean invalid = FormatUtil.isValidUUID("not-a-uuid"); // false

Description:

Method Return values Description
validUTF8(byte[] input) boolean Returns true if the given byte array is a valid UTF-8 sequence.

Description:

Method Return values Description
fixUTF8(byte[] input) byte[] Replaces null bytes (0x00) with spaces (0x20) in a byte array.

Description:

Method Return values Description
fixUTF8(String input) String Replaces null Unicode characters (\u0000) with spaces and trims the result. Returns null for null input.

Description:

Method Return values Description
trimUnicodeSurrogates(String text) String Removes Unicode surrogate characters from the given string and trims it. Returns null for null input. Useful for preparing text before indexing or XML serialization.

Description:

Method Return values Description
stripNonValidXMLCharacters(String in) String Removes all characters that are not valid in XML 1.0. Returns an empty string for null or empty input. Use this before inserting arbitrary text into an XML document.

Example:

String cleaned = FormatUtil.stripNonValidXMLCharacters("Hello\u0008World");
System.out.println(cleaned); // "HelloWorld"