title: How to recognize a datetime format?
body: <p>Datetime objects come to my program in two different formats: as unix timestamps and as <code>yyyy-MM-dd HH:mm:ss.S</code>. For example, <code>1520877600</code> or <code>2018-04-23 11:12:00.0</code>. I need to extract a year and month from these objects, automatically recognizing a format.</p>
  
  <p>This is the function that extracts <code>year</code> from <code>yyyy-MM-dd HH:mm:ss.S</code>:</p>
  
  <pre><code>  def getYear(datetimeString: Any): Int = {
      var year = 2017
      if (!datetimeString.toString.isEmpty) {
        val dateFormat = "yyyy-MM-dd HH:mm:ss.S"
        val dtf = java.time.format.DateTimeFormatter.ofPattern(dateFormat)
        val d = java.time.LocalDate.parse(datetimeString.toString, dtf)
        year = d.getYear
      }
      year
    } 
  </code></pre>
  
  <p>And this is the same function for the unix timestamp:</p>
  
  <pre><code>  def getYear(timestamp: Any): Int = {
      var year = 2017
      if (!timestamp.toString.isEmpty)
      {
        year = new DateTime(timestamp.toString.toLong).getYear
      }
      year
    }
  </code></pre>
  
  <p>How can I merge them into a single function, so that my program would be flexible and would work with both formats?</p>