I’m basing myself on in his comment:
... database saved in this format "2017-11-13" and in my program I have to convert a string data = "13-11-2017" to the database formed
I mean, at first you have a String
which contains a date in a certain format. Then the first thing to do is to transform this String
in a java.util.Date
(this process is called Parsing).
First you create a SimpleDateFormat
with the same format as String
, which in case is "day-month-year". And then you use the method parse
, passing to String
that you want parse (that is, turn into 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’ve got the Date
, you can turn it into another String
with the format you want. For this you need to create another SimpleDateFormat
with the desired format, which in this case is "year-month-day" (this process is called formatting, so we use the method format
):
// 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 that, the String
formatoFinal
will have the value 2017-11-13
.
Date
or String
?
But I still have one question: the field you’re saving is a String
or a Date
? If it is a String
, then you use the formatoFinal
above.
But if it’s a Date
, then you don’t need to convert, just save the object Date
directly. That because the Date
does not have a format. What happens is, when you make one select in the database, it shows the date in some specific format, but if the field is defined as a Date
, you don’t have to worry about this format while saving.
java time.
If you are using Java 8 (or 9, or 10...) you can use the API java.time
, that is younger and better than Date
, in several respects.
Anyway, in your case the code is very similar, since it is a simpler use case of the API. The idea is the same: to transform a String
in date, a method of Parsing, and to turn the date into String
, a formatting method is used.
The difference is that the 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 one java.time.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);
Note 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")
.
Indeed, in this particular case, the String
2017-11-13
is in the ISO 8601 format, and all classes of java.time
use this format as default. Then you could just do:
String formatoFinal = date.toString(); // 2017-11-13
If you use Java 6 or 7, you can use LocalDate
and DateTimeFormatter
through the Threeten Backport, one backport for the Java 8 classes. The only difference is that in Java 8 the classes are in the package java.time
, while in the backport the package is org.threeten.bp
.
Obs: when saving dates in the database, I recommend using the available date and time types instead of using Strings
. So you can directly save the Date
, for example. And in the newer drivers (if I’m not mistaken, from JDBC 4.2) you can even work directly with the classes from the java.time
.
I am sure this has been answered before https://answall.com/search?tab=votes&q=SimpleDateFormat
– Maniero
I don’t think so, I really need to carry out this process
– fabricio b.