-2

I have a String as "2015-12-07" and I want to convert it to java.util.Date. Is it possible? If yes, please tell how. I am using this date to insert into MySQL column of DATETIME type using Hibernate.

How property for it in entity class is:

MyEntity.java

@Column(name="xyz")
@Temporal(TemporalType.TIMESTAMP)
private java.util.Date date;
//setter and getter for date

In main I am doing like this:

class Test{
public static void main(String[] args) throws ParseException{
String str="2015-12-07";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=sdf.parse(str);
MyEntity me=new MyEntity();
me.setDate(date);
//persisting into db

}
}

what happens is code runs fine but nothing is being inserted into column

aurelius
  • 3,946
  • 7
  • 40
  • 73
jcool
  • 314
  • 1
  • 3
  • 11

2 Answers2

1

Use this:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(dateString);
codeaholicguy
  • 1,671
  • 1
  • 11
  • 18
1
String testDate = "2015-12-07";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse(testDate);
System.out.println(date);
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36