8

I am trying to use new SimpleDateFormat to parse a string in the format dd-MM. Basically, I want to create a date object out of the string and persist in the database.

When I checked the database entry I see that it appends 1970 to the year column. I believe it is the default value of the year provided when it is empty. Is there a way to prevent the year value. I do not want to store information about the year.

My code -

String dateOfBirth = "14-Feb";
dbObject.save(new SimpleDateFormat("dd-MMM").parse(dateOfBirth));

For the sake of simplicity, assume dbObject.save() the method expects a date object to be provided. I do not want to create a date of value - 14-Feb-1970, instead it should be just 14-Feb.

Thilina Dharmasena
  • 2,243
  • 2
  • 19
  • 27
Boudhayan Dev
  • 970
  • 4
  • 14
  • 42
  • You're just looking for a different data structure. I'd save it as is (string), or create a custom 2-field class. – ernest_k Apr 28 '19 at 07:36
  • String pattern = "MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat.format(new Date()); System.out.println(date); please find the code – Hasnain Ali Bohra Apr 28 '19 at 07:36
  • @HasnainAliBohra The date variable is of type string. I am storing it as date object in db. – Boudhayan Dev Apr 28 '19 at 07:41
  • @ernest_k . cannot change the data structure unfortunately. The date column needs to be there as I am using Hibernate to store this in db – Boudhayan Dev Apr 28 '19 at 07:46
  • `SimpleDateFormat` is notoriously troublesome and long outdated. Don’t use it. Also if you are using the `date` datatype of your database, there’s hardly a way to avoid also storing a year. – Ole V.V. Apr 29 '19 at 11:34
  • @OleV.V. What is the alternative for SimpleDateFormat ? – Boudhayan Dev Apr 30 '19 at 04:06
  • @BoudhayanDev The short answer is `DateTimeFormatter` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Use with the other classes from that API, it’s all so nice to work with. I think that Sweeper has given [a good and helpful answer](https://stackoverflow.com/a/55888093/5772882). – Ole V.V. Apr 30 '19 at 07:52

1 Answers1

6

I would strongly suggest you use the java.time.MonthDay class to store your dates. If your database doesn't support storing that, you can just store it as a string, and parse it when you get it out of the database.

Here is how you would parse your date:

MonthDay md = MonthDay.parse("14-Feb", DateTimeFormatter.ofPattern("dd-MMM").withLocale(Locale.US));

You can then store the string returned by .toString into the database (it will be something like --02-14), and the next time you parse it, you don't need a date time formatter:

MonthDay md = MonthDay.parse("--02-14");
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Hi, is MonthDay compatible with Hibernate ? So I am using Spring+ Hibernate to store this value in database. The column is defined as Date ( SQL) – Boudhayan Dev Apr 28 '19 at 07:43
  • @BoudhayanDev See [this answer](https://stackoverflow.com/a/46039236/5133585). It says it supports types like `LocalDate` and `LocalDateTime` but I don't know about `MonthDay`. Try it and see, I guess. – Sweeper Apr 28 '19 at 07:47
  • Looked into it. Seems like LocalDate also adds a year if not provided – Boudhayan Dev Apr 28 '19 at 08:00
  • @BoudhayanDev Did you try `MonthDay`? Does it not work with hibernate? – Sweeper Apr 28 '19 at 08:01
  • 1
    Yes it does not work . I tried to change the hibernate column which was defined of type Date to MonthDate and it gave the following error - `The persistent field or property for a Temporal type must be of type java.util.Date, java.util.Calendar or java.util.GregorianCalendar` – Boudhayan Dev Apr 28 '19 at 08:09
  • @BoudhayanDev then I guess you can only store it as a string. – Sweeper Apr 28 '19 at 08:11
  • I cannot store it as string. Because, in spring boot repository, there are some sql queries running on this field which query the date column based on month criteria. I cannot apply these queries in the date column is changed to string – Boudhayan Dev Apr 28 '19 at 08:14
  • 2
    @BoudhayanDev I found [this](https://vladmihalcea.com/java-yearmonth-jpa-hibernate/), which tries to store a `YearMonth`. A `MonthDay` is kind of similar. Maybe that'll help? I am not familiar with hibernate so I'm afraid I can't help you much more. – Sweeper Apr 28 '19 at 08:17
  • 1
    There is no SQL-standard data type for a month-day. Trying to use the SQL `DATE` type is a misfit, a hack. I suggest instead that you store the month-day as text in a varchar column, using the standard ISO 8601 format seen in this Answer: `--MM-DD`. Notice that such text when sorted alphabetically is also chronological. In Java, such a string can be parsed and generated by the `MonthDay` class, as shown in this Answer. – Basil Bourque Apr 28 '19 at 13:26
  • So you are requiring that no year is stored, and at the same time requiring that you can use SQL queries that in turn require a full date (year, month and day of month). Obviously you cannot have that. – Ole V.V. Apr 29 '19 at 11:37
  • @OleV.V. Yes that was the plan. – Boudhayan Dev Apr 30 '19 at 04:08
  • 1
    @BasilBourque Yes thanks. So, currently storing the day and month as separate values. As a result of that cannot use native SQL queries on them. So, have to move all the processing logic to Java layer. Hacky solution, but it works – Boudhayan Dev Apr 30 '19 at 04:09