Building Mobile Enterprise Applications : webMethods Mobile Development Help : Code Snippets : How to use the DateFormatter and DateTransformer
How to use the DateFormatter and DateTransformer
When working with data driven applications, it is a common use case to convert a Date object into a String or a String into a Date based on a given pattern. The recommended way to handle date format conversions is using the class com.softwareag.mobile.md.text.DateFormat. If required, DateFormat also allows to use the Timezone offset and the Locale identifier for the conversions. The Locale is important, if you need to convert date strings, which contain the name of a month or a day.
Simple example for using DateFormat:
public String formatDate(java.util.Date date) {
DateFormat dateFormat = new DateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate = dateFormat.format(date);
return formattedDate;
}

public java.util.Date parseDateString(String dateString) throws ParseException {
DateFormat dateFormat = new DateFormat("dd.MM.yyyy");
java.util.Date date = dateFormat.parse(dateString);
return date;
}
There is also a new Transformer implementation as wrapper for DateFormat, that allows date conversions in code as well as in the application model file (for example in ContentAdapter or Template bindings). The transformer implementation com.softwareag.mobile.runtime.toolkit.transformer.DateTransformer implements com.softwareag.mobile.runtime.toolkit.delegates.IDataTransformer and can therefore be used in all transformer definitions in the application model. It can also be used programmatically, as you can see in the snippets below:
public String formatDate(java.util.Calendar calendar) {
DateTransformer dateTransformer = new DateTransformer("dd.MM.yy");
String formattedDate = (String) dateTransformer.transform(calendar.getTime(),
IDataTransformer.TYPE_STRING);
return formattedDate;
}

public java.util.Date parseDateString(String dateString) {
DateTransformer dateTransformer = new DateTransformer("dd.MM.yy");
java.util.Date date = (java.util.Date) dateTransformer.transform(dateString,
IDataTransformer.TYPE_DATE);
return date;
}
Copyright © 2007-2017 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback