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.
Size formatting
formatSize
Description:
| Method | Return values | Description |
|---|---|---|
|
formatSize(long bytes) |
String |
Formats a byte count as a human-readable size string (e.g. |
|
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();
}
}
}
parseSize
Description:
| Method | Return values | Description |
|---|---|---|
|
parseSize(String text) |
long |
Parses a human-readable size string (e.g. |
|
text: The size string to parse. |
||
Example:
long bytes = FormatUtil.parseSize("10MB");
System.out.println(bytes); // 10485760
Date formatting
formatDate (Calendar)
Description:
| Method | Return values | Description |
|---|---|---|
|
formatDate(Calendar cal) |
String |
Formats a |
Example:
import java.util.Calendar;
import com.openkm.util.FormatUtil;
System.out.println(FormatUtil.formatDate(Calendar.getInstance())); // "15-03-2024 14:30:00"
formatDate (Date)
Description:
| Method | Return values | Description |
|---|---|---|
|
formatDate(Date date) |
String |
Formats a |
formatDate (Unix epoch)
Description:
| Method | Return values | Description |
|---|---|---|
|
formatDate(long unixEpoch) |
String |
Formats a Unix epoch value (milliseconds since 1970-01-01) as |
Example:
System.out.println(FormatUtil.formatDate(System.currentTimeMillis())); // "15-03-2024 14:30:00"
Time interval formatting
formatSeconds
Description:
| Method | Return values | Description |
|---|---|---|
|
formatSeconds(long time) |
String |
Formats a duration in milliseconds as |
formatSecondsSince
Description:
| Method | Return values | Description |
|---|---|---|
|
formatSecondsSince(long time) |
String |
Formats the elapsed time since the given start timestamp (in milliseconds) as |
formatMilliSeconds
Description:
| Method | Return values | Description |
|---|---|---|
|
formatMilliSeconds(long time) |
String |
Formats a duration in milliseconds as |
formatMilliSecondsSince
Description:
| Method | Return values | Description |
|---|---|---|
|
formatMilliSecondsSince(long time) |
String |
Formats the elapsed time since the given start timestamp (in milliseconds) as |
Example:
long start = System.currentTimeMillis();
// ... long operation ...
System.out.println("Elapsed: " + FormatUtil.formatMilliSecondsSince(start)); // "00:00:01.234"
String formatting
formatArray
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 |
formatObject
Description:
| Method | Return values | Description |
|---|---|---|
|
formatObject(Object value) |
String |
Converts an object to a string. For |
HTML and input sanitization
escapeHtml
Description:
| Method | Return values | Description |
|---|---|---|
|
escapeHtml(String str) |
String |
Escapes |
sanitizeInput
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 |
cleanXSS
Description:
| Method | Return values | Description |
|---|---|---|
|
cleanXSS(String value) |
String |
Cleans a string by escaping HTML angle brackets and parentheses, removing |
sanitizeEmailString
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");
// "<b>Hello</b><br/>World"
String sanitized = FormatUtil.sanitizeInput("<script>alert('xss')</script>Hello");
System.out.println(sanitized); // "Hello"
String and encoding validation
isValidUUID
Description:
| Method | Return values | Description |
|---|---|---|
|
isValidUUID(String str) |
boolean |
Returns |
Example:
boolean valid = FormatUtil.isValidUUID("d50133e3-dbfa-4d01-a109-28785cd48f40"); // true
boolean invalid = FormatUtil.isValidUUID("not-a-uuid"); // false
validUTF8
Description:
| Method | Return values | Description |
|---|---|---|
|
validUTF8(byte[] input) |
boolean |
Returns |
fixUTF8 (byte array)
Description:
| Method | Return values | Description |
|---|---|---|
|
fixUTF8(byte[] input) |
byte[] |
Replaces null bytes ( |
fixUTF8 (String)
Description:
| Method | Return values | Description |
|---|---|---|
|
fixUTF8(String input) |
String |
Replaces null Unicode characters ( |
trimUnicodeSurrogates
Description:
| Method | Return values | Description |
|---|---|---|
|
trimUnicodeSurrogates(String text) |
String |
Removes Unicode surrogate characters from the given string and trims it. Returns |
stripNonValidXMLCharacters
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"