Greetings to all,
I have the following algorithm;
import org.joda.time.DateTime;
import org.joda.time.Days;
public class DataDirenciada {
public static void main(String[] args) {
DateTime entrada = new DateTime(2015, 1, 1, 17, 30);
DateTime audiencia = new DateTime(2015, 2, 20, 14, 0);
int dias = Days.daysBetween(entrada, audiencia).getDays();
System.out.println("Quantidade de dias: " + dias);
}
}
And that's the result;
Number of days: 49
Actually there is nothing wrong with algorithm, but pay close attention to these lines of code;
DateTime entrada = new DateTime(2015, 1, 1, 17, 30);
How do I get DateTime to receive a date in this format 12/02/2015?
I wanted to receive in the format mentioned and at the same time have the same result as the application
I know there is this method below;
SimpleDateFormat formatoBrasileiro = new SimpleDateFormat("dd/MM/yyyy");
But I do not know how to use it.
I tried to do this, but it did not work, it caused an error;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.Days;
public class DataDirenciada {
public static void main(String[] args) {
SimpleDateFormat formatoBrasileiro = new SimpleDateFormat("dd/MM/yyyy");
String dataStringInicio = "12/01/2015";
String dataStringFinal = "14/01/2015";
try {
Date dataInicios = formatoBrasileiro.parse(dataStringInicio);
Date dataFinal = formatoBrasileiro.parse(dataStringFinal);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DateTime entrada = new DateTime(dataStringInicio);
DateTime audiencia = new DateTime(dataStringFinal);
int dias = Days.daysBetween(entrada, audiencia).getDays();
System.out.println("Quantidade de dias: " + dias);
}
}
this was the error;
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "12/01/2015" is malformed at "/01/2015"
at org.joda.time.format.DateTimeParserBucket.doParseMillis(DateTimeParserBucket.java:187)
at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:780)
at org.joda.time.convert.StringConverter.getInstantMillis(StringConverter.java:65)
at org.joda.time.base.BaseDateTime.<init>(BaseDateTime.java:175)
at org.joda.time.DateTime.<init>(DateTime.java:257)
at com.java.exercicios.DataDirenciada.main(DataDirenciada.java:28)