Working with Converted Dates

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;

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)
    
asked by anonymous 14.07.2015 / 16:37

2 answers

2

wladyband

The error that occurs is in this part here:

  DateTime entrada = new DateTime(dataStringInicio);  //<--mandou uma string
  DateTime audiencia = new DateTime(dataStringFinal); //<--mandou uma string

This is because DateTime does not accept the default that its String is as an argument (The standards it accepts are described here ). You created the formatted dates there but did not use them.

SimpleDateFormat has the pattern you want, and by giving parse you ask it to return a Date according to your pattern.

Make the following change:

 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);

               DateTime entrada = new DateTime(dataInicios.getTime());  
               DateTime audiencia = new DateTime(dataFinal.getTime());  

              int dias = Days.daysBetween(entrada, audiencia).getDays();  

               System.out.println("Quantidade de dias: " + dias); 

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }  

If that does not suit you. Take a look at constructors of class DateTime .

    
14.07.2015 / 19:32
1

Save!

Does this solve?

public static void main(String[] args) {
    DateTime entrada = DateTimeFormat.forPattern("dd/MM/yyyy").parseDateTime("12/02/2015");
    DateTime audiencia = DateTimeFormat.forPattern("dd/MM/yyyy").parseDateTime("12/03/2015");

    int dias = Days.daysBetween(entrada, audiencia).getDays();

    System.out.println("Quantidade de dias: " + dias);
}

Output:

Quantidade de dias: 28

If you want to change the input pattern, simply follow the Formatter pattern. Ex: dd/MM/yyyy HH:mm:ss

    
14.07.2015 / 19:32