How to save in Date type in this format 10-10-2017 using SimpleDateFormat?

1

How can I save a Date in the format 10-10-2017 using SimpleDateFormat ?

Date data = null;
SimpleDateFormat formato = new SimpleDateFormat("yyyy/MM/dd");
try {
    data = formato.parse(dataInicio);

} catch (ParseException e) {
    e.printStackTrace();
}

promocao.setData_inicio(formato.format(data));
    
asked by anonymous 13.11.2017 / 19:48

2 answers

0

I'm basing myself in your comment :

  

... database is saved in this format "2017-11-13" and in my program I have to convert a string data="13-11-2017" to the bank formed

That is, at first you have a String that contains a date in a certain format. So the first thing to do is turn this String into a java.util.Date (this process is called parsing ).

First you create a SimpleDateFormat with the same format as String , which in this case is "day-month-year". And then you use the parse method, passing the String you want to parse (that is, to Date ):

String data = "13-11-2017";

// criar um SimpleDateFormat com o mesmo formato da String (dia-mês-ano)
SimpleDateFormat parser = new SimpleDateFormat("dd-MM-yyyy");
// cria o Date, baseado na String
Date date = parser.parse(data);

Now that you already have Date , you can turn it into another String with the format you want. For this you need to create other SimpleDateFormat with the desired format, which in this case is "year-month-day" (this process is called formatting, so we use the format method) :

// criar um SimpleDateFormat com o formato da String que eu quero converter
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
// cria a String
String formatoFinal = formatter.format(date);

With this, the String formatoFinal will have the value 2017-11-13 .

Date or String ?

But I still had a question: is the field you are saving a String or a Date ? If it is a String , then you use formatoFinal above.

But if it's a Date , then you do not need to convert, just save the Date object directly. This because Date does not have a format . What happens is that when you do a select in the database, it shows the date in some specific format, but if the field is set to Date , you do not have to worry about this format when saving.

java.time

If you are using Java 8 (or 9, or 10 ...) you can use the java.time API, which is newer and better than Date in many ways.

Anyway, in your case the code is very similar, since it is a case of simpler use of the API. The idea is the same: to convert a String to a date, a parsing method is used, and to convert the date to String , a formatting method is used. p>

The difference is that java.time has several different types for each situation. In your case, you are only working with day, month, and year, so the best option is to use LocalDate :

String data = "13-11-2017";
// transformar a String em um LocalDate
// criar um DateTimeFormatter com o mesmo formato da String
DateTimeFormatter parser = DateTimeFormatter.ofPattern("dd-MM-uuuu");
LocalDate date = LocalDate.parse(data, parser);

// transformar o LocalDate em String com o outro formato
// criar um DateTimeFormatter com o formato da String que eu quero converter
// no caso, estou usando uma constante que já retorna o que eu quero
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
// cria a String
String formatoFinal = formatter.format(date);

Notice that I used an existing constant ( ISO_LOCAL_DATE ), which has the format I want (year-month-day), but I could also use DateTimeFormatter.ofPattern("yyyy-MM-dd") .

In fact, in this specific case, String 2017-11-13 is in the ISO 8601 format , and all classes of java.time use this format as default . So you could just do it:

String formatoFinal = date.toString(); // 2017-11-13

If you use Java 6 or 7, you can use LocalDate and DateTimeFormatter through ThreeTen Backport , a backport for the Java 8 classes. The only difference is that in Java 8 the classes are in the java.time package, while in the backport the package is org.threeten.bp .

Obs : When storing dates in the database, I recommend using the available date and time types instead of using Strings . So, you can directly save Date , for example. And in newer drivers (if I'm not mistaken, from JDBC 4.2) you can even work directly with the java.time classes.

    
10.05.2018 / 14:50
0

If I understand your question, it's quite simple:

    SimpleDateFormat formato = new SimpleDateFormat("dd-MM-yyyy");
    System.out.println(formato.format(new Date()));

Obs: As the colleague there commented, probably this type of doubt has already been answered.

    
13.11.2017 / 19:57