Default format to enter MappedDateTime in text field?

61 views
Skip to first unread message

Todd O'Bryan

unread,
Sep 14, 2010, 10:27:09 PM9/14/10
to lif...@googlegroups.com
How do I enter a MappedDateTime in the text field created by CRUDify
so that I don't get NULL?

Thanks,
Todd

Naftoli Gugenheim

unread,
Sep 21, 2010, 8:38:31 PM9/21/10
to liftweb
It uses LiftRules.dateTimeConverter, which is a factory that by default uses net.liftweb.util.DefaultDateTimeConverter, which is defined as follows (references are to methods in net.liftweb.util.TimeHelpers):

object DefaultDateTimeConverter extends DateTimeConverter {
  def formatDateTime(d: Date) = internetDateFormat.format(d)
  def formatDate(d: Date) = dateFormat.format(d)
  /**  Uses Helpers.hourFormat which includes seconds but not time zone */
  def formatTime(d: Date) = hourFormat.format(d)
  
  def parseDateTime(s: String) = tryo { internetDateFormat.parse(s) }
  def parseDate(s: String) = tryo { dateFormat.parse(s) }
  /** Tries Helpers.hourFormat and Helpers.timeFormat */
  def parseTime(s: String) = tryo{hourFormat.parse(s)} or tryo{timeFormat.parse(s)}
}

Here is an example of supplying your own:

    import java.util.Date
    LiftRules.dateTimeConverter.default.set(() => new net.liftweb.util.DateTimeConverter {
      def dateTime = {
        val sdf = new java.text.SimpleDateFormat("M/d/yy hh:mm a")
        sdf.setTimeZone(java.util.TimeZone.getTimeZone("America/New_York"))
        sdf.setLenient(true)
        sdf
      }
      def date = {
        val sdf = new java.text.SimpleDateFormat("MM/dd/yyyy")
        sdf.setTimeZone(java.util.TimeZone.getTimeZone("America/New_York"))
        sdf.setLenient(true)
        sdf
      }
      def formatDateTime(d: Date) = dateTime.format(d)
      def formatDate(d: Date) = date.format(d)
      def formatTime(d: Date) = DefaultDateTimeConverter.formatTime(d)

      def parseDateTime(s: String) = Helpers.tryo { dateTime.parse(s) }
      def parseDate(s: String) = Helpers.tryo { date.parse(s) }
      def parseTime(s: String) = DefaultDateTimeConverter.parseTime(s)

    })


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.


Todd O'Bryan

unread,
Sep 21, 2010, 9:39:12 PM9/21/10
to lif...@googlegroups.com
Thanks for taking the time to answer that. I eventually got through
the code and found my answer and should have posted back when I did.
(I had to print out a date so I could tell what it was looking for.)

Todd

Steve Swing

unread,
Sep 23, 2010, 6:26:34 PM9/23/10
to Lift
Naftoli,

Thanks for replying with this. I thought I had the same issue except
with Record (DateTimeField). I see now how it's not implemented the
same way (not using LiftRules.dateTimeConverter). Should it be changed
to be consistent? Right now I don't see how Record and DateTimeField
are customizable in the same way. Am I missing something? Perhaps
TimeHelpers needs to be aware of LiftRules.dateTimeConverter?

Thanks,

Steve

Naftoli Gugenheim

unread,
Sep 26, 2010, 11:46:04 PM9/26/10
to liftweb
I would assume it should, although I haven't really used Record and so can't really comment.



--
Reply all
Reply to author
Forward
0 new messages