Reputation: 1215
So the date I am inserting(which is inside another object) is 2018/04/26,parsed from a String.Before I save it with debugging I can see it is correct,but when I request it from the database it shows as 2018/04/25.Any idea what is going on here?The database is an Amazon Web Services MySql RDS
This is my schedule class,inside it is a list of DateActiveScheduleItems which contains a date property.I am initializing the date like this
**Date date1= new SimpleDateFormat("yyyy/MM/dd").parse(suppliedString)**
Schedule
@Entity
public class Schedule {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@ManyToOne
@JoinColumn(name = "usergroup_id")
private UserGroup userGroup;
private String description;
private boolean master;//is this a schedule for all supervalus(true) or one supervalu(false)
@OneToMany(mappedBy = "schedule",cascade = CascadeType.ALL)
private List<DateActiveScheduleItem> dateActiveScheduleItems = new ArrayList<>();
@OneToMany(mappedBy = "schedule",cascade = CascadeType.PERSIST)
private List<MusicScheduleItem> musicScheduleItems = new ArrayList<>();
@OneToMany(mappedBy = "schedule",cascade = CascadeType.PERSIST)
private List<AdvertisementScheduleItem> advertisementScheduleItems = new ArrayList<>();
@Basic
@Temporal(TemporalType.DATE)
private java.util.Date dateAdded;}
DateActiveScheduleItem
@Entity
public class DateActiveScheduleItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Basic
@Temporal(TemporalType.DATE)
private Date date;
@JsonIgnore
@ManyToOne
@JoinColumn(name = "schedule_id")
private Schedule schedule;}
I am saving the object using the .save() method from hibernate CrudRepository.
Strangely enough the "date added" property in my schedule which I set to be "now" inserts correctly,which I initialize slightly different. Maybe I could try initialize my date for dateactiveitem this way but not sure how?
Calendar now = Calendar.getInstance();
schedule.setDateAdded(now.getTime());
Upvotes: 0
Views: 51
Reputation: 21
may be server timezone? Check below thread - he had same problem.
Joda Time - Hibernate inserts yesterday's date into database
Upvotes: 2