所以我要插入的日期(在另一个对象中)是2018/04/26,从字符串解析而来。在我用调试保存它之前,我可以看到它是正确的,但是当我从数据库中请求它时,它显示为2018/04/25。你知道这是怎么回事吗?数据库是amazon web services mysql rds
这是我的schedule类,里面是包含日期属性的dateactivescheduleitems列表
**Date date1= new SimpleDateFormat("yyyy/MM/dd").parse(suppliedString)**
日程安排
@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;}
我正在使用hibernate crudepository的.save()方法保存对象。
奇怪的是,我的日程表中的“date added”属性被设置为“now”时插入正确,而我初始化的这个属性略有不同。也许我可以尝试用这种方式初始化dateactiveitem的日期,但不确定如何初始化?
Calendar now = Calendar.getInstance();
schedule.setDateAdded(now.getTime());
1条答案
按热度按时间hi3rlvi21#
可能是服务器时区?仔细检查——他也有同样的问题。
joda time-hibernate将昨天的日期插入数据库