Utility Context

  • A simple helper API to perform various handy operations

  • Context Variable: util

Example

To get the current time in milliseconds
def millis = util.millis();
To format a date in milliseconds to a desired format
def formatted = util.format("mm/dd/yyyy-HH:mm:ss.SSS", 1629086350975);

API Interface

//Converts a Map<Strng,Object> to JSON String
String toString(Map<Strng,Object> map);

//Converts a Map<Strng,Object> to JSON String with pretty formatted
String toString(Map<Strng,Object> map, boolean pretty);

//Exception strack trace as string
String toString(Throwable t);

//Converts a JSONArray to List<Object>
List<Object> toList(JSONArray jsonArray);

//Converts to a Map<Strng,Object>
Map<String, Object> toMap(String json);

//Converts to a Map<Strng,Object>
Map<String, Object> toMap(com.mashape.unirest.http.JsonNode json);

//Converts to a Map<Strng,Object>
Map<String, Object> toMap(org.codehaus.jettison.json.JSONObject jsonObject);

//Converts to a Map<Strng,Object>
Map<String, Object> toMap(com.fasterxml.jackson.databind.JsonNode json);

//Converts to a Map<Strng,Object>
Map<String, Object> toMap(org.json.JSONObject json);

//Get the server epoch milliseconds
long millis();

//Format the current system time into a Date string
String formatDate(String pattern);

//Format the time millis into a Date string
String formatDate(String pattern, long millis);

//Converts a string to byte[] using BASE64 decoding
byte[] decodeBase64(String value);

//Converts the HEX string into byte[]
byte[] decodeHex(String value);

//Restores a UUID from string representation
UUID uuid(String uuid);

//Generates a random UUID
UUID uuid();

//Converts a Map<Strng,Object> to JSONObject
JSONObject toJson(Map<Strng,Object> map);

//Parse the given data string using the supplied pattern and return as milliseconds
long parseDate(String pattern, String date);

APIs Inherited From

Context Base