From f8d792b70d327752d8b8b83dfe60abaca66d6f81 Mon Sep 17 00:00:00 2001 From: Xavier GUIHOT Date: Thu, 18 Jan 2018 09:58:50 +0100 Subject: [PATCH] Add method checking if a date is complient with a given format --- README.md | 8 +++---- build.sbt | 2 +- docs/com/spark_helper/DateHelper$.html | 24 +++++++++++++++++++ docs/index/index-i.html | 3 +++ .../scala/com/spark_helper/DateHelper.scala | 22 +++++++++++++++++ .../com/spark_helper/DateHelperTest.scala | 11 +++++++++ 6 files changed, 65 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6d924e9..67694a8 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Overview -Version: 1.0.17 +Version: 1.0.18 API Scaladoc: [SparkHelper](http://xavierguihot.com/spark_helper/#com.spark_helper.SparkHelper$) @@ -131,7 +131,7 @@ With sbt, add these lines to your build.sbt: ```scala resolvers += "jitpack" at "https://jitpack.io" -libraryDependencies += "com.github.xavierguihot" % "spark_helper" % "v1.0.17" +libraryDependencies += "com.github.xavierguihot" % "spark_helper" % "v1.0.18" ``` With maven, add these lines to your pom.xml: @@ -147,7 +147,7 @@ With maven, add these lines to your pom.xml: com.github.xavierguihot spark_helper - v1.0.17 + v1.0.18 ``` @@ -161,7 +161,7 @@ allprojects { } dependencies { - compile 'com.github.xavierguihot:spark_helper:v1.0.17' + compile 'com.github.xavierguihot:spark_helper:v1.0.18' } ``` diff --git a/build.sbt b/build.sbt index 0b671b7..a83544f 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ name := "spark_helper" -version := "1.0.17" +version := "1.0.18" scalaVersion := "2.11.8" diff --git a/docs/com/spark_helper/DateHelper$.html b/docs/com/spark_helper/DateHelper$.html index e46565d..b27b4a5 100644 --- a/docs/com/spark_helper/DateHelper$.html +++ b/docs/com/spark_helper/DateHelper$.html @@ -378,6 +378,30 @@

Definition Classes
AnyRef → Any
+
  • + + +

    + + + def + + + isDateCompliantWithFormat(stringValue: String, format: String): Boolean + +

    + + Permalink + + +

    Validates a string date is under the provided format.

    Validates a string date is under the provided format.

    assert(DateHelper.isDateCompliantWithFormat("20170302", "yyyyMMdd"))
    +assert(!DateHelper.isDateCompliantWithFormat("20170333", "yyyyMMdd"))
    +assert(DateHelper.isDateCompliantWithFormat("20170228", "yyyyMMdd"))
    +assert(!DateHelper.isDateCompliantWithFormat("20170229", "yyyyMMdd"))
    +assert(!DateHelper.isDateCompliantWithFormat("170228", "yyyyMMdd"))
    +assert(!DateHelper.isDateCompliantWithFormat("", "yyyyMMdd"))
    +assert(!DateHelper.isDateCompliantWithFormat("a", "yyyyMMdd"))
    +assert(!DateHelper.isDateCompliantWithFormat("24JAN17", "yyyyMMdd"))
    stringValue

    the stringified date

    returns

    if the provided date is under the provided format

  • diff --git a/docs/index/index-i.html b/docs/index/index-i.html index a20a301..18a4dd0 100644 --- a/docs/index/index-i.html +++ b/docs/index/index-i.html @@ -11,6 +11,9 @@
    +
    isDateCompliantWithFormat
    + +
    isHdfsXmlCompliantWithXsd
    diff --git a/src/main/scala/com/spark_helper/DateHelper.scala b/src/main/scala/com/spark_helper/DateHelper.scala index bff1f12..96a2fc8 100644 --- a/src/main/scala/com/spark_helper/DateHelper.scala +++ b/src/main/scala/com/spark_helper/DateHelper.scala @@ -3,6 +3,8 @@ package com.spark_helper import org.joda.time.{DateTime, DateTimeZone, Days} import org.joda.time.format.{DateTimeFormat, DateTimeFormatter} +import scala.util.Try + /** A facility which deals with usual date needs (wrapper around joda-time). * * The goal is to remove the maximum of highly used low-level code from your @@ -358,4 +360,24 @@ object DateHelper extends Serializable { def dayOfWeek(date: String, format: String = "yyyyMMdd"): Int = { DateTimeFormat.forPattern(format).parseDateTime(date).getDayOfWeek() } + + /** Validates a string date is under the provided format. + * + * {{{ + * assert(DateHelper.isDateCompliantWithFormat("20170302", "yyyyMMdd")) + * assert(!DateHelper.isDateCompliantWithFormat("20170333", "yyyyMMdd")) + * assert(DateHelper.isDateCompliantWithFormat("20170228", "yyyyMMdd")) + * assert(!DateHelper.isDateCompliantWithFormat("20170229", "yyyyMMdd")) + * assert(!DateHelper.isDateCompliantWithFormat("170228", "yyyyMMdd")) + * assert(!DateHelper.isDateCompliantWithFormat("", "yyyyMMdd")) + * assert(!DateHelper.isDateCompliantWithFormat("a", "yyyyMMdd")) + * assert(!DateHelper.isDateCompliantWithFormat("24JAN17", "yyyyMMdd")) + * }}} + * + * @param stringValue the stringified date + * @return if the provided date is under the provided format + */ + def isDateCompliantWithFormat(stringValue: String, format: String): Boolean = { + Try(DateTimeFormat.forPattern(format).parseDateTime(stringValue)).isSuccess + } } diff --git a/src/test/scala/com/spark_helper/DateHelperTest.scala b/src/test/scala/com/spark_helper/DateHelperTest.scala index 815ec2d..d327b3b 100644 --- a/src/test/scala/com/spark_helper/DateHelperTest.scala +++ b/src/test/scala/com/spark_helper/DateHelperTest.scala @@ -73,4 +73,15 @@ class DateHelperTest extends FunSuite { test("Day of Week") { assert(DateHelper.dayOfWeek("20180102") === 2) } + + test("Date versus Provided Format") { + assert(DateHelper.isDateCompliantWithFormat("20170302", "yyyyMMdd")) + assert(!DateHelper.isDateCompliantWithFormat("20170333", "yyyyMMdd")) + assert(DateHelper.isDateCompliantWithFormat("20170228", "yyyyMMdd")) + assert(!DateHelper.isDateCompliantWithFormat("20170229", "yyyyMMdd")) + assert(!DateHelper.isDateCompliantWithFormat("170228", "yyyyMMdd")) + assert(!DateHelper.isDateCompliantWithFormat("", "yyyyMMdd")) + assert(!DateHelper.isDateCompliantWithFormat("a", "yyyyMMdd")) + assert(!DateHelper.isDateCompliantWithFormat("24JAN17", "yyyyMMdd")) + } }