Package lazyj

Class Format

java.lang.Object
lazyj.Format

public final class Format
extends java.lang.Object
Utility class providing lots of useful or less useful functions.
Since:
2006-10-13
Author:
costing
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  Format.JSONFragment  
  • Constructor Summary

    Constructors 
    Constructor Description
    Format()  
  • Method Summary

    Modifier and Type Method Description
    static java.lang.String byteToHex​(byte val)
    Get the 2 hexa digit representation of this byte
    static java.lang.String decode​(java.lang.String text)
    Decode an URL-encoded string, using the UTF-8 charset.
    static java.lang.String encode​(java.lang.String text)
    Produce an URL encoding of the given text, using the UTF-8 charset
    static java.lang.String escHtml​(java.lang.String text)
    Transform a text into an HTML-safe string.
    static java.lang.String escJS​(java.lang.String text)
    Create a JS-safe string representation.
    static java.lang.String escJSON​(java.lang.String text)
    Create a JSON-safe string representation.
    static java.lang.String escSQL​(java.lang.String text)
    Create a SQL-safe version of the given text, to be embedded into SQL queries
    static java.lang.String extractAddress​(java.lang.String address)
    From a full mail address (eg.
    static java.lang.String extractMailTitle​(java.lang.String address)
    From a full mail address (eg.
    static java.lang.String formatBR​(java.lang.String text)
    Put HTML line breaks in the place of normal text line breaks.
    static java.lang.String formatN​(java.lang.String text)
    Reverse function of the formatBR(String), converts HTML line breaks into text line breaks
    static char hexChar​(int val)
    Get the hexa digit for this integer value
    static java.util.Date parseDate​(java.lang.String s)
    Transform a string that represents a Date into a real Date object.
    static java.lang.String point​(double number)
    Show a nice number, with the number of decimal places chosen automatically depending on the number to format
    static java.lang.String replace​(java.lang.String s, java.lang.String sWhat, java.lang.String sWith)
    Replace a sequence of text with another sequence in an original string
    static java.lang.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 already
    static java.lang.String showDate​(java.util.Date d)
    Show the date and time in a nice human-readable format
    static java.lang.String showDottedDate​(java.util.Date d)
    Show a date in a dotted manner (dd.MM.yyyy)
    static java.lang.String showDottedDouble​(double d)
    Show the value in 3 digit groups separated by commas, no period point
    static java.lang.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 point
    static java.lang.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 java.lang.String showDottedLong​(long l)
    Show the value in 3 digit groups separated by commas
    static java.lang.String showLongNamedDate​(java.util.Date d)
    Show the full month name (11 January 2006)
    static java.lang.String showNamedDate​(java.util.Date d)
    Show the abreviated month name (11 Jan 2006)
    static java.lang.String showNiceDate​(java.util.Date d)
    Get a nifty string representation for a date.
    static java.lang.String showTime​(java.util.Date d)
    Show the time only (12:34)
    static java.lang.String size​(double dim)
    Nicely show the size of a file.
    static java.lang.String size​(double size, java.lang.String unit)
    Nicely show a size, starting from a given measurement unit.
    static java.lang.String size​(long dim)
    Nicely show the size of a file.
    static java.lang.String size​(long size, java.lang.String unit)
    Nicely show a size, starting from a given measurement unit.
    static java.lang.String stackTraceToString​(java.lang.Throwable t)
    Get the stack trace as String
    static java.lang.String toCommaList​(java.util.Collection<java.lang.Integer> values)
    Convert a collection of numbers in a comma-separated list.
    static java.lang.String toInterval​(long lInterval)
    Convert a time in milliseconds to a human readable interval display
    static Format.JSONFragment toJSON​(java.lang.Object o)
    Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them.
    static Format.JSONFragment toJSON​(java.lang.Object o, boolean newLines)
    Get a JSON representation of base object types (String, Number, Boolean) and collections (Iterable, Map) of them.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • size

      public static java.lang.String size​(long dim)
      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(double, String)
    • size

      public static java.lang.String size​(long size, java.lang.String unit)
      Nicely show a size, starting from a given measurement unit.
      Parameters:
      size - file size, in the specified unit
      unit - 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

      public static java.lang.String size​(double dim)
      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(double, String)
    • size

      public static java.lang.String size​(double size, java.lang.String unit)
      Nicely show a size, starting from a given measurement unit.
      Parameters:
      size - file size, in the specified unit
      unit - 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

      public static java.lang.String point​(double number)
      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

      public static java.lang.String extractMailTitle​(java.lang.String address)
      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

      public static java.lang.String extractAddress​(java.lang.String address)
      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

      public static java.lang.String escHtml​(java.lang.String text)
      Transform a text into an HTML-safe string.
      Parameters:
      text - original text
      Returns:
      the HTML-safe version of the text
    • encode

      public static java.lang.String encode​(java.lang.String text)
      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

      public static java.lang.String decode​(java.lang.String text)
      Decode an URL-encoded string, using the UTF-8 charset.
      Parameters:
      text - text to decode
      Returns:
      plain text version
    • escSQL

      public static java.lang.String escSQL​(java.lang.String text)
      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

      public static java.lang.String escJS​(java.lang.String text)
      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

      public static java.lang.String escJSON​(java.lang.String text)
      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

      public static java.lang.String replace​(java.lang.String s, java.lang.String sWhat, java.lang.String sWith)
      Replace a sequence of text with another sequence in an original string
      Parameters:
      s - original text
      sWhat - what to search and replace
      sWith - the new text to put in place
      Returns:
      the modified text
    • formatBR

      public static final java.lang.String formatBR​(java.lang.String text)
      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

      public static final java.lang.String formatN​(java.lang.String text)
      Reverse function of the formatBR(String), converts HTML line breaks into text line breaks
      Parameters:
      text - original text
      Returns:
      text with \n instead of <BR>
    • parseDate

      public static final java.util.Date parseDate​(java.lang.String s)
      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

      public static final java.lang.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 already
      Parameters:
      i - the number to format
      Returns:
      0-padded string representation
    • showNiceDate

      public static final java.lang.String showNiceDate​(java.util.Date d)
      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

      public static final java.lang.String showDate​(java.util.Date d)
      Show the date and time in a nice human-readable format
      Parameters:
      d - date to represent
      Returns:
      date and time
    • showDottedDate

      public static final java.lang.String showDottedDate​(java.util.Date d)
      Show a date in a dotted manner (dd.MM.yyyy)
      Parameters:
      d - date to show
      Returns:
      the dotted date
    • showLongNamedDate

      public static final java.lang.String showLongNamedDate​(java.util.Date d)
      Show the full month name (11 January 2006)
      Parameters:
      d - date to show
      Returns:
      date with full month name
    • showNamedDate

      public static final java.lang.String showNamedDate​(java.util.Date d)
      Show the abreviated month name (11 Jan 2006)
      Parameters:
      d - date to show
      Returns:
      date with short month name
    • showTime

      public static final java.lang.String showTime​(java.util.Date d)
      Show the time only (12:34)
      Parameters:
      d - date to show
      Returns:
      the hours and minutes only
    • showDottedLong

      public static final java.lang.String showDottedLong​(long l)
      Show the value in 3 digit groups separated by commas
      Parameters:
      l - value to show
      Returns:
      nice string representation
    • showDottedDouble

      public static final java.lang.String showDottedDouble​(double d)
      Show the value in 3 digit groups separated by commas, no period point
      Parameters:
      d - value to show
      Returns:
      nice string representation
    • showDottedDouble

      public static final java.lang.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 point
      Parameters:
      d - value to show
      dotplaces - number of decimal places after the point
      Returns:
      nice string representation
    • showDottedDouble

      public static final java.lang.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.
      Parameters:
      dbl - value to show
      decimals - number of decimal places after the point
      aproximated - whether or not to use "millions" and "bilions" for very large numbers
      Returns:
      nice string representation
    • toInterval

      public static final java.lang.String toInterval​(long lInterval)
      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

      public static final java.lang.String byteToHex​(byte val)
      Get the 2 hexa digit representation of this byte
      Parameters:
      val -
      Returns:
      the 2 hexa digit representation
    • stackTraceToString

      public static final java.lang.String stackTraceToString​(java.lang.Throwable t)
      Get the stack trace as String
      Parameters:
      t - the exception to get the trace for
      Returns:
      the stack trace as String
    • toCommaList

      public static java.lang.String toCommaList​(java.util.Collection<java.lang.Integer> values)
      Convert a collection of numbers in a comma-separated list.
      Parameters:
      values - values to export
      Returns:
      comma-separated list
    • toJSON

      public static Format.JSONFragment toJSON​(java.lang.Object o)
      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 their toString() output in JSON.
      Parameters:
      o - Object to serialize
      Returns:
      the JSON string
    • toJSON

      public static Format.JSONFragment toJSON​(java.lang.Object o, boolean newLines)
      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 their toString() output in JSON.
      Parameters:
      o - Object to serialize
      newLines - whether or not to spread the output in nice, one value per line format, or in compact form
      Returns:
      the JSON string