Convert string to Calendar

Asked

Viewed 5,140 times

3

Well I need to do a String to Calendar conversion and I have to set the value in a variable (that is in type Calendar) in a class and then insert the data in a database!

System.out.println("Digite a Data de Aniversario do Funcionario");
 String data = scan1.nextLine();
 SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd");
 Date data1 =(Date)form.parse(data);
 Calendar cal = Calendar.getInstance();
 cal.setTime(data1);
 f.setDataAniver(cal.setTime(data1));
  • What’s the doubt? The code you presented is already converting to Calendar. Click on [Edit] and detail what’s the doubt you have.

  • the problem is that when entering into the database it does not insert correctly! gets the data incorrectly

  • So you want to convert Calendar back to date?

  • 1

    Wander, welcome to [en.so]! As already stated, the code presented in the question already does what the question asks.I imagine that you created this question assuming that the problem was in this passage, but this assumption was wrong. I suggest you create another question and this time put the problem that is really happening and the snippet of code where it occurs. Hug.

  • JDBC, standard database access API with Java does not accept Calendar, only java.sql.Date or java.sql.Timestamp. Anyway, you need to tell what technology you are using and post the snippet where you insert the data into the database, because as I said, the problem is not the conversion from string to calendar.

  • @utluiz Thanks for the info! I am using Hibernate! I managed to solve my problem, it was a lack of attention from me with the code presented.

Show 1 more comment

1 answer

3


Assuming the column where you want to record this information is of the type Date and while using JDBC, you can convert the format Calendar for java.sql.Date right in the setDate() of your PrepareStatement, take the example:

    //...

    pstm.setDate(new java.sql.Date(f.getDataAniver().getTimeInMillis()));

    //...

No need to format as yyyy/MM/dd, even if the date value enters the Brazilian format or other custom format in the Calendar, while converting using java.sql.Date, the standard formatting(ISO 8601) type will be applied, see in the example below:

    String strDate = "01/07/1990";
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Calendar c = Calendar.getInstance();     
    c.setTime(sdf.parse(strDate));
    //aqui está o "pulo do gato"
    System.out.println(new java.sql.Date(c.getTimeInMillis()));

Exhibiting:

1990-07-01

See an example above working on IDEONE.

  • Almost that.... My column is in the Date type, only first I add the date in a variable of the Calendar class and then insert it in the database. I’m using Hibernate..

  • @Wanderlopes the answer perfectly meets this. See the example of the conversion of the type Calendar to the type date at the end of the question. If it’s not that, then you’re having a hard time exposing your doubt.

  • 1

    Thanks, completely solved my problem! Just a lack of attention in the code, after analyzing calmly I found my mistake. thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.