Skip to content

Commit

Permalink
Add method checking if a date is complient with a given format
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierguihot committed Jan 18, 2018
1 parent a0b5bb1 commit f8d792b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$)

Expand Down Expand Up @@ -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:
Expand All @@ -147,7 +147,7 @@ With maven, add these lines to your pom.xml:
<dependency>
<groupId>com.github.xavierguihot</groupId>
<artifactId>spark_helper</artifactId>
<version>v1.0.17</version>
<version>v1.0.18</version>
</dependency>
```

Expand All @@ -161,7 +161,7 @@ allprojects {
}
dependencies {
compile 'com.github.xavierguihot:spark_helper:v1.0.17'
compile 'com.github.xavierguihot:spark_helper:v1.0.18'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "spark_helper"

version := "1.0.17"
version := "1.0.18"

scalaVersion := "2.11.8"

Expand Down
24 changes: 24 additions & 0 deletions docs/com/spark_helper/DateHelper$.html
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,30 @@ <h4 class="signature">
</a>
</span>
<div class="fullcomment"><dl class="attributes block"> <dt>Definition Classes</dt><dd>AnyRef → Any</dd></dl></div>
</li><li name="com.spark_helper.DateHelper#isDateCompliantWithFormat" visbl="pub" data-isabs="false" fullComment="yes" group="Ungrouped">
<a id="isDateCompliantWithFormat(stringValue:String,format:String):Boolean"></a>
<a id="isDateCompliantWithFormat(String,String):Boolean"></a>
<h4 class="signature">
<span class="modifier_kind">
<span class="modifier"></span>
<span class="kind">def</span>
</span>
<span class="symbol">
<span class="name">isDateCompliantWithFormat</span><span class="params">(<span name="stringValue">stringValue: <span class="extype" name="scala.Predef.String">String</span></span>, <span name="format">format: <span class="extype" name="scala.Predef.String">String</span></span>)</span><span class="result">: <span class="extype" name="scala.Boolean">Boolean</span></span>
</span>
</h4><span class="permalink">
<a href="../../index.html#com.spark_helper.DateHelper$@isDateCompliantWithFormat(stringValue:String,format:String):Boolean" title="Permalink" target="_top">
<img src="../../lib/permalink.png" alt="Permalink" />
</a>
</span>
<p class="shortcomment cmt">Validates a string date is under the provided format.</p><div class="fullcomment"><div class="comment cmt"><p>Validates a string date is under the provided format.</p><pre>assert(DateHelper.isDateCompliantWithFormat(<span class="lit">"20170302"</span>, <span class="lit">"yyyyMMdd"</span>))
assert(!DateHelper.isDateCompliantWithFormat(<span class="lit">"20170333"</span>, <span class="lit">"yyyyMMdd"</span>))
assert(DateHelper.isDateCompliantWithFormat(<span class="lit">"20170228"</span>, <span class="lit">"yyyyMMdd"</span>))
assert(!DateHelper.isDateCompliantWithFormat(<span class="lit">"20170229"</span>, <span class="lit">"yyyyMMdd"</span>))
assert(!DateHelper.isDateCompliantWithFormat(<span class="lit">"170228"</span>, <span class="lit">"yyyyMMdd"</span>))
assert(!DateHelper.isDateCompliantWithFormat(<span class="lit">""</span>, <span class="lit">"yyyyMMdd"</span>))
assert(!DateHelper.isDateCompliantWithFormat(<span class="lit">"a"</span>, <span class="lit">"yyyyMMdd"</span>))
assert(!DateHelper.isDateCompliantWithFormat(<span class="lit">"24JAN17"</span>, <span class="lit">"yyyyMMdd"</span>))</pre></div><dl class="paramcmts block"><dt class="param">stringValue</dt><dd class="cmt"><p>the stringified date</p></dd><dt>returns</dt><dd class="cmt"><p>if the provided date is under the provided format</p></dd></dl></div>
</li><li name="scala.Any#isInstanceOf" visbl="pub" data-isabs="false" fullComment="yes" group="Ungrouped">
<a id="isInstanceOf[T0]:Boolean"></a>
<a id="isInstanceOf[T0]:Boolean"></a>
Expand Down
3 changes: 3 additions & 0 deletions docs/index/index-i.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

</head>
<body><div class="entry">
<div class="name">isDateCompliantWithFormat</div>
<div class="occurrences"><a href="../com/spark_helper/DateHelper$.html" class="extype" name="com.spark_helper.DateHelper">DateHelper</a> </div>
</div><div class="entry">
<div class="name">isHdfsXmlCompliantWithXsd</div>
<div class="occurrences"><a href="../com/spark_helper/HdfsHelper$.html" class="extype" name="com.spark_helper.HdfsHelper">HdfsHelper</a> </div>
</div><div class="entry">
Expand Down
22 changes: 22 additions & 0 deletions src/main/scala/com/spark_helper/DateHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://www.joda.org/joda-time/apidocs/">joda-time</a>).
*
* The goal is to remove the maximum of highly used low-level code from your
Expand Down Expand Up @@ -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
}
}
11 changes: 11 additions & 0 deletions src/test/scala/com/spark_helper/DateHelperTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
}

0 comments on commit f8d792b

Please sign in to comment.