Package lazyj
Class Format
java.lang.Object
lazyj.Format
Utility class providing lots of useful or less useful functions.
- Since:
- 2006-10-13
- Author:
- costing
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final String
byteToHex
(byte val) Get the 2 hexa digit representation of this bytestatic String
Decode an URL-encoded string, using the UTF-8 charset.static String
Produce an URL encoding of the given text, using the UTF-8 charsetstatic String
Transform a text into an HTML-safe string.static String
Create a JS-safe string representation.static String
Create a JSON-safe string representation.static String
Create a SQL-safe version of the given text, to be embedded into SQL queriesstatic String
extractAddress
(String address) From a full mail address (eg.static String
extractMailTitle
(String address) From a full mail address (eg.static final String
Put HTML line breaks in the place of normal text line breaks.static final String
Reverse function of theformatBR(String)
, converts HTML line breaks into text line breaksstatic final char
hexChar
(int val) Get the hexa digit for this integer valuestatic final Date
Transform a string that represents a Date into a real Date object.static String
point
(double number) Show a nice number, with the number of decimal places chosen automatically depending on the number to formatstatic String
Replace a sequence of text with another sequence in an original stringstatic final String
show0
(int i) For date formatting, put a "0" in front of a single digit, or leave the number as it is if there are at least two digits alreadystatic final String
Show the date and time in a nice human-readable formatstatic final String
Show a date in a dotted manner (dd.MM.yyyy)static final String
showDottedDouble
(double d) Show the value in 3 digit groups separated by commas, no period pointstatic final String
showDottedDouble
(double d, int dotplaces) Show the value in 3 digit groups separated by commas, with the specified number of decimal places after the decimal pointstatic final String
showDottedDouble
(double dbl, int decimals, boolean aproximated) Show the value in 3 digit groups separated by commas, with the specified number of decimal places after the decimal point.static final String
showDottedLong
(long l) Show the value in 3 digit groups separated by commasstatic final String
Show the full month name (11 January 2006)static final String
Show the abreviated month name (11 Jan 2006)static final String
showNiceDate
(Date d) Get a nifty string representation for a date.static final String
Show the time only (12:34)static String
size
(double dim) Nicely show the size of a file.static String
Nicely show a size, starting from a given measurement unit.static String
size
(long dim) Nicely show the size of a file.static String
Nicely show a size, starting from a given measurement unit.static final String
Get the stack trace as Stringstatic String
toCommaList
(Collection<Integer> values) Convert a collection of numbers in a comma-separated list.static final String
toInterval
(long lInterval) Convert a time in milliseconds to a human readable interval displaystatic Format.JSONFragment
Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them.static Format.JSONFragment
Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them.
-
Constructor Details
-
Format
public Format()
-
-
Method Details
-
size
Nicely show the size of a file. Something like 12.1KB, 0.75MB ...- Parameters:
dim
- file size, in bytes- Returns:
- the nice human-readable size
- See Also:
-
size
Nicely show a size, starting from a given measurement unit.- Parameters:
size
- file size, in the specified unitunit
- unit. Can be nothing(==B) or one of the following: B, K, M, G, T, P- Returns:
- nice human-readable size
- Since:
- 1.0.7
-
size
Nicely show the size of a file. Something like 12.1KB, 0.75MB ...- Parameters:
dim
- file size, in bytes- Returns:
- the nice human-readable size
- Since:
- 1.0.7
- See Also:
-
size
Nicely show a size, starting from a given measurement unit.- Parameters:
size
- file size, in the specified unitunit
- unit. Can be nothing(==B) or one of the following: B, K, M, G, T, P- Returns:
- nice human-readable size
- Since:
- 1.0.5
-
point
Show a nice number, with the number of decimal places chosen automatically depending on the number to format- Parameters:
number
-- Returns:
- nice floating point number representation
- Since:
- 1.0.5
-
extractMailTitle
From a full mail address (eg. "Full Name <account@server.com>") try to extract the full name. If it cannot extract the full name it will return the mail address.- Parameters:
address
- full address- Returns:
- the name of the person
-
extractAddress
From a full mail address (eg. "Full Name" <account@server.com>) try to extract the email address.- Parameters:
address
- full address- Returns:
- the address part or
null
if there is no email part in it
-
escHtml
Transform a text into an HTML-safe string.- Parameters:
text
- original text- Returns:
- the HTML-safe version of the text
-
encode
Produce an URL encoding of the given text, using the UTF-8 charset- Parameters:
text
- text to encode- Returns:
- URL-safe version of the text
-
decode
Decode an URL-encoded string, using the UTF-8 charset.- Parameters:
text
- text to decode- Returns:
- plain text version
-
escSQL
Create a SQL-safe version of the given text, to be embedded into SQL queries- Parameters:
text
- original text- Returns:
- SQL-safe version of the text
-
escJS
Create a JS-safe string representation. This is useful when you want to pass a text to a dynamic string variable in the final HTML document.- Parameters:
text
- original text- Returns:
- JS-string-safe version
-
escJSON
Create a JSON-safe string representation. This is useful when you want to pass a text to a dynamic string variable in the final HTML document.- Parameters:
text
- original text- Returns:
- JSON-string-safe version, as per http://json.org/
-
replace
Replace a sequence of text with another sequence in an original string- Parameters:
s
- original textsWhat
- what to search and replacesWith
- the new text to put in place- Returns:
- the modified text
-
formatBR
Put HTML line breaks in the place of normal text line breaks.- Parameters:
text
- original text- Returns:
- text with <BR&gr; instead of the \n in the original text
-
formatN
Reverse function of theformatBR(String)
, converts HTML line breaks into text line breaks- Parameters:
text
- original text- Returns:
- text with \n instead of <BR>
-
parseDate
Transform a string that represents a Date into a real Date object. It will try several formats, including date-only and time-only representations.- Parameters:
s
- string to convert- Returns:
- date representation, or null if the conversion was not possible.
-
show0
For date formatting, put a "0" in front of a single digit, or leave the number as it is if there are at least two digits already- Parameters:
i
- the number to format- Returns:
- 0-padded string representation
-
showNiceDate
Get a nifty string representation for a date. Something like "today", "yesterday", "21 May 2006" ...- Parameters:
d
- date to convert- Returns:
- nice human readable date
-
showDate
Show the date and time in a nice human-readable format- Parameters:
d
- date to represent- Returns:
- date and time
-
showDottedDate
Show a date in a dotted manner (dd.MM.yyyy)- Parameters:
d
- date to show- Returns:
- the dotted date
-
showLongNamedDate
Show the full month name (11 January 2006)- Parameters:
d
- date to show- Returns:
- date with full month name
-
showNamedDate
Show the abreviated month name (11 Jan 2006)- Parameters:
d
- date to show- Returns:
- date with short month name
-
showTime
Show the time only (12:34)- Parameters:
d
- date to show- Returns:
- the hours and minutes only
-
showDottedLong
Show the value in 3 digit groups separated by commas- Parameters:
l
- value to show- Returns:
- nice string representation
-
showDottedDouble
Show the value in 3 digit groups separated by commas, no period point- Parameters:
d
- value to show- Returns:
- nice string representation
-
showDottedDouble
Show the value in 3 digit groups separated by commas, with the specified number of decimal places after the decimal point- Parameters:
d
- value to showdotplaces
- number of decimal places after the point- Returns:
- nice string representation
-
showDottedDouble
Show the value in 3 digit groups separated by commas, with the specified number of decimal places after the decimal point.- Parameters:
dbl
- value to showdecimals
- number of decimal places after the pointaproximated
- whether or not to use "millions" and "bilions" for very large numbers- Returns:
- nice string representation
-
toInterval
Convert a time in milliseconds to a human readable interval display- Parameters:
lInterval
- interval time in milliseconds- Returns:
- human readable interval
-
hexChar
public static final char hexChar(int val) Get the hexa digit for this integer value- Parameters:
val
- value- Returns:
- hexa digit
-
byteToHex
Get the 2 hexa digit representation of this byte- Parameters:
val
-- Returns:
- the 2 hexa digit representation
-
stackTraceToString
Get the stack trace as String- Parameters:
t
- the exception to get the trace for- Returns:
- the stack trace as String
-
toCommaList
Convert a collection of numbers in a comma-separated list.- Parameters:
values
- values to export- Returns:
- comma-separated list
-
toJSON
Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them. The method deals with user classes in a primitive way, simply encapsulating theirtoString()
output in JSON.- Parameters:
o
- Object to serialize- Returns:
- the JSON string
-
toJSON
Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them. The method deals with user classes in a primitive way, simply encapsulating theirtoString()
output in JSON.- Parameters:
o
- Object to serializenewLines
- whether or not to spread the output in nice, one value per line format, or in compact form- Returns:
- the JSON string
-