src/main/scala/net/tz/lift/util/YmdDateTimeConverter.scala
author Tomas Zeman <tzeman@volny.cz>
Thu, 19 May 2011 14:31:58 +0200
changeset 17 e55d1e65f6d4
parent 2 a1a5ebc06887
permissions -rw-r--r--
tz-lift-misc: 0.2 -> 0.3-SNAPSHOT
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     1
/*
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     2
 * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     3
 *
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     4
 * Licensed under the Apache License, Version 2.0 (the "License");
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     5
 * you may not use this file except in compliance with the License.
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     6
 * You may obtain a copy of the License at
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     7
 *
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     8
 *     http://www.apache.org/licenses/LICENSE-2.0
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     9
 *
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    10
 * Unless required by applicable law or agreed to in writing, software
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    11
 * distributed under the License is distributed on an "AS IS" BASIS,
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    13
 * See the License for the specific language governing permissions and
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    14
 * limitations under the License.
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    15
 */
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    16
package net.tz.lift.util
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    17
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    18
import java.text.SimpleDateFormat
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    19
import java.util.Date
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    20
import net.liftweb.common._
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    21
import net.liftweb.util.DateTimeConverter
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    22
import net.liftweb.util.TimeHelpers._
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    23
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    24
object YmdDateTimeConverter extends DateTimeConverter {
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    25
  val df = new SimpleDateFormat("yyyy-MM-dd")
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    26
  val dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    27
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    28
  def formatDateTime(d: Date) = dtf.format(d)
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    29
  def formatDate(d: Date) = df.format(d)
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    30
  /**  Uses Helpers.hourFormat which includes seconds but not time zone */
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    31
  def formatTime(d: Date) = hourFormat.format(d)
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    32
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    33
  def parseDateTime(s: String) = tryo { dtf.parse(s) }
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    34
  def parseDate(s: String) = tryo { df.parse(s) }
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    35
  /** Tries Helpers.hourFormat and Helpers.timeFormat */
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    36
  def parseTime(s: String) =
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    37
    tryo{hourFormat.parse(s)} or tryo{timeFormatter.parse(s)}
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    38
}
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    39
a1a5ebc06887 YYYY-MM-DD date time converter
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    40
// vim: set ts=2 sw=2 et: