Example usage:
String to date
DateConverter.stringToDate( '12.12.2001', 'dd.MM.yyyy' ) -> java.util.Date
Date to string
DateConverter.dateToString( @Now(), 'dd.MM.yyyy hh:mm' ) -> 08.04.2010 12:01
var DateConverter = {
dateToString: function( date:java.util.Date, pattern:String ){
try {
if( !date ){ return ''; }
var formatter = DateConverter.getFormatter( pattern );
return formatter.format( date );
} catch( e ){
// ErrorHandling
}
},
stringToDate: function( dateString:String, pattern:String ){
try {
if( !dateString ){ return null; }
var formatter = DateConverter.getFormatter( pattern );
return formatter.parse( dateString );
} catch( e ){
// ErrorHandling
}
},
getFormatter: function( pattern:String ){
try {
var cacheKey = 'dateFormatter' + pattern;
var dateFormatter = applicationScope[ cacheKey ];
if( !dateFormatter ){
dateFormatter = new java.text.SimpleDateFormat( pattern );
applicationScope[ cacheKey ] = dateFormatter;
}
return dateFormatter;
} catch( e ){
// ErrorHandling
}
}
}
6 comments:
Thanks for this!! This has saved me a bunch of time since there is no @Format function.
Was starting to work on my own as I stumbled upon yours.
Saved my some work! Appreciate it! =)
wonderful bit of code. THANK YOU for posting it.
-Devin.
Turns out there's a built-in date formatter in the I18n library:
I18n.toString(compositeData.date, 'yyyy-MM-dd');
Came in handy. Thank you.
Somehow this is not working for me. Can you please share sample code where you guys have used this ?
Post a Comment