Convert String to Calendar

1

I'm trying to do a conversion from String to Calendar but to no avail.

My String is in dd / MM / yyyy format. I need to convert to yyyy-MM-dd And set in a Calendar type object.   void setDATAFUNDACAO(java.util.Calendar value);

Here is my current code

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse("07/04/2016");
    Calendar cal =  sdf.getCalendar();

But unfortunately it throws this exception

  

Exception in thread "main" java.text.ParseException: Unparseable date:   "07/04/2016" at java.text.DateFormat.parse (DateFormat.java:337) at   test.main (test.java:54)

    
asked by anonymous 08.04.2016 / 01:19

1 answer

2

If you have set SimpleDateFormat to "yyyy-MM-dd", this is the format you should use in the parse command:

Date date = sdf.parse("2016-07-04");

That's why exception . The code below worked for me:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse("07/04/2016");
Calendar cal =  sdf.getCalendar();

cal.setTime(date);

String df = sdf2.format(date);

System.out.println(cal.getTime());

System.out.println(df);
    
08.04.2016 / 01:41