org.olat.core.util
Class Formatter

java.lang.Object
  extended by org.olat.core.logging.LogDelegator
      extended by org.olat.core.util.Formatter

public class Formatter
extends LogDelegator

enclosing_type Description:
A formatter to format locale-specific things (mainly dates and times)

Author:
Felix Jost

Method Summary
static java.lang.StringBuilder escapeDoubleQuotes(java.lang.String source)
          Escape " with \" in strings
static java.lang.StringBuilder escapeSingleAndDoubleQuotes(java.lang.String source)
          Deprecated. use org.apache.commons.lang.StringEscapeUtils.escapeJavaScript() instead.
static java.lang.StringBuilder escWithBR(java.lang.String source)
           
 java.lang.String formatDate(java.util.Date d)
          formats the given date so it is friendly to read
 java.lang.String formatDateAndTime(java.util.Date d)
          formats the given date so it is friendly to read
static java.lang.String formatDatetime(java.util.Date d)
          Formats the given date with the ISO 8601 standard also known as 'datetime' See http://www.w3.org/TR/NOTE-datetime.html for more info.
static java.lang.String formatDatetimeFilesystemSave(java.util.Date d)
          Use this for naming files or directories with a timestamp.
static java.lang.String formatDuration(long millis)
          Formats a duration in millis to "XXh YYm ZZs"
static java.lang.String formatLatexFormulas(java.lang.String htmlFragment)
          Wrapp given html code with a wrapper an add code to transform latex formulas to nice visual characters on the client side.
 java.lang.String formatTime(java.util.Date d)
          formats the given time period so it is friendly to read
 java.lang.String formatTimeShort(java.util.Date d)
          formats the given time period so it is friendly to read
static java.lang.String formatWikiMarkup(java.lang.String oldValue)
          Deprecated. 
static Formatter getInstance(java.util.Locale locale)
          get an instance of the Formatter given the locale
 java.util.Locale getLocale()
           
 java.lang.String getSimpleDatePatternForDate()
          Generate a simple date pattern that formats a date using the locale of the formatter
 java.lang.String getSimpleDatePatternForDateAndTime()
          Generate a simple date pattern that formats a date with time using the locale of the formatter
static void main(java.lang.String[] args)
          some old testing
static java.lang.String makeStringFilesystemSave(java.lang.String source)
          replaces all non ASCII characters in a string and also the most common special characters like by urlencode it "/" "\" ":" "*" "?" """ ' "<" ">" "|"
 java.lang.StringBuilder replace(java.lang.String source, java.lang.String delim, java.lang.String replacement)
          replace a given String with a different String
static double round(double value, int decimalPlace)
          Round a double value to a double value with given number of figures after comma
static float round(float value, int decimalPlace)
          Round a float value to a float value with given number of figures after comma
static java.lang.String roundToString(float value, int decimalPlace)
          Format a float as string with given number of figures after comma
static java.lang.StringBuilder stripTabsAndReturns(java.lang.String source)
           
static java.lang.String truncate(java.lang.String source, int len)
          truncates the supplied string to len-3 an replaces the last three positions with ...
static java.lang.String truncate(java.lang.String source, int len, java.lang.String delim)
          truncates a String: useful to limit in GUI
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static Formatter getInstance(java.util.Locale locale)
get an instance of the Formatter given the locale

Parameters:
locale - the locale which the formatter should use in its operations
Returns:
the instance of the Formatter

formatDate

public java.lang.String formatDate(java.util.Date d)
formats the given date so it is friendly to read

Parameters:
d - the date
Returns:
a String with the formatted date

formatTime

public java.lang.String formatTime(java.util.Date d)
formats the given time period so it is friendly to read

Parameters:
d - the date
Returns:
a String with the formatted time

formatDateAndTime

public java.lang.String formatDateAndTime(java.util.Date d)
formats the given date so it is friendly to read

Parameters:
d - the date
Returns:
a String with the formatted date and time

getSimpleDatePatternForDate

public java.lang.String getSimpleDatePatternForDate()
Generate a simple date pattern that formats a date using the locale of the formatter

Returns:

getSimpleDatePatternForDateAndTime

public java.lang.String getSimpleDatePatternForDateAndTime()
Generate a simple date pattern that formats a date with time using the locale of the formatter

Returns:

formatDatetime

public static java.lang.String formatDatetime(java.util.Date d)
Formats the given date with the ISO 8601 standard also known as 'datetime' See http://www.w3.org/TR/NOTE-datetime.html for more info.

Parameters:
d - the date to be formatted
Returns:
a String with the formatted date and time

formatDatetimeFilesystemSave

public static java.lang.String formatDatetimeFilesystemSave(java.util.Date d)
Use this for naming files or directories with a timestamp. As windows does not like ":" in filenames formatDateAndTime(d) does not work

Parameters:
d - the date to be formatted
Returns:
a String with the formatted date and time

formatTimeShort

public java.lang.String formatTimeShort(java.util.Date d)
formats the given time period so it is friendly to read

Parameters:
d - the date
Returns:
a String with the formatted time

formatDuration

public static java.lang.String formatDuration(long millis)
Formats a duration in millis to "XXh YYm ZZs"

Parameters:
millis -
Returns:
formatted string

escapeDoubleQuotes

public static java.lang.StringBuilder escapeDoubleQuotes(java.lang.String source)
Escape " with \" in strings

Parameters:
source -
Returns:
escaped string

escapeSingleAndDoubleQuotes

public static java.lang.StringBuilder escapeSingleAndDoubleQuotes(java.lang.String source)
Deprecated. use org.apache.commons.lang.StringEscapeUtils.escapeJavaScript() instead.

Escape " with \" and ' with \' in strings

Parameters:
source -
Returns:
escaped string

replace

public java.lang.StringBuilder replace(java.lang.String source,
                                       java.lang.String delim,
                                       java.lang.String replacement)
replace a given String with a different String

Parameters:
source - the source
delim - the String to replace
replacement - the replacement String
Returns:
StringBuilder

truncate

public static java.lang.String truncate(java.lang.String source,
                                        int len)
truncates the supplied string to len-3 an replaces the last three positions with ...

Parameters:
source -
len -
Returns:
truncated string

makeStringFilesystemSave

public static java.lang.String makeStringFilesystemSave(java.lang.String source)
replaces all non ASCII characters in a string and also the most common special characters like by urlencode it "/" "\" ":" "*" "?" """ ' "<" ">" "|"

Parameters:
source -
Returns:
a string which is OS independant and save for using on any filesystem

truncate

public static java.lang.String truncate(java.lang.String source,
                                        int len,
                                        java.lang.String delim)
truncates a String: useful to limit in GUI

Parameters:
source -
len - length of the returned string; if negative, return n chars from the end of the string, otherwise from the beginning of the string
delim -
Returns:
truncated string

main

public static void main(java.lang.String[] args)
some old testing

Parameters:
args -

getLocale

public java.util.Locale getLocale()
Returns:
locale of this formatter

escWithBR

public static java.lang.StringBuilder escWithBR(java.lang.String source)
Parameters:
source -
Returns:
escaped string

stripTabsAndReturns

public static java.lang.StringBuilder stripTabsAndReturns(java.lang.String source)
Parameters:
source -
Returns:
stripped string

formatWikiMarkup

@Deprecated
public static java.lang.String formatWikiMarkup(java.lang.String oldValue)
Deprecated. 

renders wiki markup like _italic_ to XHTML see also www.radeox.org

Parameters:
originalText -
Returns:
result (rendered originalText) or null if originalText was null

formatLatexFormulas

public static java.lang.String formatLatexFormulas(java.lang.String htmlFragment)
Wrapp given html code with a wrapper an add code to transform latex formulas to nice visual characters on the client side. The latex formulas must be within an HTML element that has the class 'math' attached.

Parameters:
htmlFragment - A html element that might contain an element that has a class 'math' with latex formulas
Returns:

round

public static double round(double value,
                           int decimalPlace)
Round a double value to a double value with given number of figures after comma

Parameters:
value -
decimalPlace -
Returns:
rounded double value

round

public static float round(float value,
                          int decimalPlace)
Round a float value to a float value with given number of figures after comma

Parameters:
value -
decimalPlace -
Returns:
rounded float value

roundToString

public static java.lang.String roundToString(float value,
                                             int decimalPlace)
Format a float as string with given number of figures after comma

Parameters:
value -
decimalPlace -
Returns:
formatted string