0
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;
Quantity of days: 49
There’s actually nothing wrong with an algorithm, but pay close attention to those 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?
wanted to receive in the format mentioned and at the same time have the same result of the application
I know there is this method below;
SimpleDateFormat formatoBrasileiro = new SimpleDateFormat("dd/MM/yyyy");
But I don’t know how to use it.
I tried to do it that way, but it didn’t work, it made a mistake;
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);
}
}
that was the mistake;
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)
Perhaps that would be: http://answall.com/q/74558/3117 ?
– Math
What is wrong with Simpledateformat? wants to convert from String to Date, Data.sql to Data.util, Data to String?
– Wellington Avelino
take a look, I just updated my post
– wladyband