0

I am trying to parse a date in scala like below

scala> import java.text.SimpleDateFormat
import java.text.SimpleDateFormat

scala> import java.util.Calendar
import java.util.Calendar

scala> val simpleDateFormat = new SimpleDateFormat("YYYYMMDD")
simpleDateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@1e3cd860

scala> val date = simpleDateFormat.parse("20180312")
date: java.util.Date = Sun Dec 31 00:00:00 PST 2017

What am I missing here?

yalkris
  • 2,596
  • 5
  • 31
  • 51
  • 1
    Avoid the `SimpleDateFormat` class. It’s not only long outdate (aloing with `Calendar` and `Date`), it is also notoriously troublesome. Instead use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). It is so much nicer to work with. – Ole V.V. Mar 14 '18 at 05:46
  • 1
    Possible duplicate of [Java SimpleDateFormat always returning January for Month](https://stackoverflow.com/questions/3560279/java-simpledateformat-always-returning-january-for-month) – Ole V.V. Mar 14 '18 at 05:49
  • Another related question: [Printing Date object without using format method](https://stackoverflow.com/questions/47477718/printing-date-object-without-using-format-method) (about formatting, not parsing, otherwise the problem is the same). – Ole V.V. Mar 14 '18 at 06:04

2 Answers2

1

You have to use yyyyMMdd. Notice MM is in caps.

Scala REPL

scala> val simpleDateFormat = new SimpleDateFormat("yyyyMMdd")
simpleDateFormat: java.text.SimpleDateFormat = java.text.SimpleDateFormat@ef87e460

scala> val date = simpleDateFormat.parse("20180312")
date: java.util.Date = Mon Mar 12 00:00:00 EET 2018
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
0

I am giving you Java code trusting that you can translate to Scala:

    LocalDate date = LocalDate.parse("20180312", DateTimeFormatter.BASIC_ISO_DATE);
    System.out.println(date);

Output:

2018-03-12

No need for the notoriously troublesome SimpleDateFormat class and its long outdated friends Calendar and Date. No need to specify a format pattern string yourself since the formatter you need is built-in. No way to get the case of format pattern letters wrong. java.time, the modern Java date and time API is giving you all this. I warmly recommend it. On top of this, the LocalDate class of java.time matches your need much more precisely than Date or Calendar: a LocalDate is a date without time-of-day.

What went wrong in your code?

Uppercase YYYY in the format pattern string is for week-based year, the year that the week number of the week belongs to (so really only useful with week numbers). MM is fine for month. Uppercase DD is for the number of the day within the year (lowercase dd is for day of month). So you asked for the week-based year that began with week 1 of 2018, month of March and the 12th day of the year. This is self-contradictory. One of the troublesome traits of SimpleDateFormat is that where it ought to throw an exception with such impossible requirements, instead it pretends that all is well and gives you some incorrect result. Let me guess, your week begins on Sunday? So apparently it gave up on month and day-of-year (since it didn’t really know the calendar year) and just gave you the first day of week 1. On my computer I got Mon Jan 01 00:00:00 CET 2018, which probably is because Monday is the first day of the week here.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161