0

I don't understand how can I save date and time for example "eventDateFrom" in MongoDB in specific format.

I try do it like that:

Part of Event class: public class Event {

@Id
private String id;
private String eventTitle;

@DateTimeFormat(pattern = "MM/dd/yyyy HH:mm:ss")
private Date eventDateFrom;
private String eventDateTo;
private Integer minEventPartcipants;
private String eventDateConfirmTo;
private String location;
private String minEventTime;
private List<Participant> participantList = new ArrayList<>();

Part of RunAtStart class:

  Event event = new Event();

    event.setEventTitle("Wyjazd na rower Turbacz");
    event.setEventDateFrom(new Date("05/27/2017 10:00:00"));
    event.setEventDateTo("27.05.2017 22:00:00");
    event.setMinEventPartcipants(2);
    event.setEventDateConfirmTo("26.05.2017 10:00:00");
    event.setLocation("Lindego 13a");
    event.setMinEventTime("06:00:00");

But the time "eventDateFrom" is wrong.

MongoDB document:

    {
"_id" : ObjectId("592d34dcc5f2fd2d204d7db4"),
"_class" : "info.wzielezicki.app.MeetHelp.model.Event",
"eventTitle" : "Wyjazd na rower Turbacz",
"eventDateFrom" : ISODate("2017-05-27T08:00:00.000Z"),
"eventDateTo" : "27.05.2017 22:00:00",
"minEventPartcipants" : 2,
"eventDateConfirmTo" : "26.05.2017 10:00:00",
"location" : "Lindego 13a",
"minEventTime" : "06:00:00",
"participantList" : []
   }
  1. What I'm doing wrong?
  2. Is the better way to define date and time format?
jocom
  • 69
  • 1
  • 3
  • 13
  • 1
    1. You're using a string. 2. Yes there is a better way, Use a BSON date. For which in Java you supply `java.util.Date`. See [How to insert a document with date in mongo?](https://stackoverflow.com/questions/24483727/how-to-insert-a-document-with-date-in-mongo) – Neil Lunn May 30 '17 at 09:40
  • 1
    Looks like a time zone problem. This post might help : https://stackoverflow.com/questions/2891361/how-to-set-time-zone-of-a-java-util-date – jvwilge May 30 '17 at 09:41
  • @jvwilge I used `@DateTimeFormat(pattern = "MM/dd/yyyy HH:mm:ss") private Date eventDateFrom;` and `TimeZone.setDefault(TimeZone.getTimeZone("UTC"));` now eventDateFrom in MongoDB is `"eventDateFrom" : ISODate("2017-05-27T10:00:00.000Z")` What do you think, it is correct sollution – jocom May 30 '17 at 10:45
  • That depends if your source time is in UTC. Poland is not UTC, but UTC+2. I personally prefer to save times and dates in UTC (and display them in your front end in the preferred time zone), this way you won't run into any problems with daylight savings time. – jvwilge May 30 '17 at 11:05
  • @jvwilge thanks for information but I still have problem. I change pattern for eventDateFrom `@DateTimeFormat(pattern = "dd/MM/yyyy HH:mm:ss"), save event again `event.setEventDateFrom(new Date("27/05/2017 10:00:00")); private Date eventDateFrom;` and now in MongoDB I can see ` "eventDateFrom" : ISODate("2019-03-05T10:00:00.000Z"),` – jocom May 30 '17 at 11:12
  • https://xkcd.com/1179/ – Jens Schauder May 30 '17 at 12:18

1 Answers1

0

If you want to store a Date in a specific format and not the one, the store uses, you don't really want to store a Date, but a String, so just make the property of type String. You can still have additional accessors that use Date to use inside your application.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • thanks for information. I want to save date set by the user in the application. I understand that it does not matter what format of date I well receive and save in my database. I have to parse this data format before doing the calculation for example eventDateFrom - eventDateTo. Am I right? – jocom May 30 '17 at 12:55
  • Yes you would need to parse it. – Jens Schauder May 31 '17 at 04:57