0

I have a very simple string of the form

YYYYMMDDHHMMSS

Basically a full date/time string. Say an example is

20170224134523

Above implies

year: 2017
month: 02
day:24
hour:13
min:45
sec:23

I want to split it so that i can have it in variables (year, month, day, hour, min, sec). This is in Scala I want to. I was thinking should I use a 6-Tuple and on the right side I will use a regex or what as the most efficient way. If I want to do it in a concise way is what I am trying to think. Little bad with regular expressions. Can anyone help?

I may want to have each variable in the 6-tuple as option type because otherwise that will also do my sanity check? Say if any variable comes out as None, I want to throw an exception

3 Answers 3

1

java.text.SimpleDateFormat handles this kind of date parsing well.

scala> val sdf = new SimpleDateFormat("yyyyMMddkkmmss")
sdf: java.text.SimpleDateFormat = java.text.SimpleDateFormat@8e10adc0

scala> val date = sdf.parse("20170224134523")
date: java.util.Date = Fri Feb 24 13:45:23 PST 2017

You can get the date, day, hours, etc from a successful parse of the date as the API shows below.

scala> res0.get
getClass   getDate   getDay   getHours   getMinutes   getMonth   getSeconds   getTime   getTimezoneOffset   getYear

Further, I'd suggest wrapping the parse call in a Try to handle the successful and unsuccessful parsing.

scala> val date = Try(sdf.parse("20170224134523"))
date: scala.util.Try[java.util.Date] = Success(Fri Feb 24 13:45:23 PST 2017)

scala> val date = Try(sdf.parse("asdf"))
date: scala.util.Try[java.util.Date] = Failure(java.text.ParseException: Unparseable date: "asdf")

Here's the same thing using the newer LocalDateTime instead of Date and it's deprecated methods.

LocalDateTime.parse("20170224134523", DateTimeFormatter.ofPattern("yMMddkkmmss"))

java.time.LocalDateTime = 2017-02-24T13:45:23
Sign up to request clarification or add additional context in comments.

3 Comments

Nice, except all those get methods have been deprecated. Is there an accepted alternative?
Java overhauled the date and time apis semi-recently. I think the new equivalent would be LocalDateTime.parse(str, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
Note that yMMddkkmmss seems to work with DateTimeFormatter as expected but does not return the desired date with SimpleDateFormat. SimpleDateFormat works with yyyyMMddkkmmss.
1

Because it is a date string it probably makes sense to use a dedicated date parsing library and parse to a datetime type. Fortunatly, java provides a very good one with the java.time package.

val dateTime = LocalDateTime.parse("20170224134523", DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))

Which will produce a LocalDateTime object (date and time without a timezone attached). If you need more complicated string parsing you can use a DateTimeFormatterBuilder to customize the date format exactly as you need it.

2 Comments

Thank you. How do I wrap this in Try and return Option(of datatime) or None if the parsing fails?
@curiousengineer This method will throw exception on error, so you can just wrap in a try as normal Try(...).toOption
0

With such a predictable format you can grab it by position using a substring function (from, to) into a date class.

The regex pattern to grab the sections as groups is:

(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})

Demo

2 Comments

But there are chances the string might be garbled too.
If you can post all the possible variants in the OP, then we can come up with a pattern that handles them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.