How to work with converted dates

Asked

Viewed 124 times

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)
  • 1

    Perhaps that would be: http://answall.com/q/74558/3117 ?

  • What is wrong with Simpledateformat? wants to convert from String to Date, Data.sql to Data.util, Data to String?

  • take a look, I just updated my post

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

That’s because the DateTime does not accept the standard that its String is as argument (The standards he accepts are described here). You created the formatted dates there but did not use them.

The SimpleDateFormat has the pattern you want, and when giving a parse you ask him to return a Date according to your standard.

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 doesn’t suit you. Take a look at us builders class DateTime.

  • by the logic of its algorithm was to work, but it is returning the amount of days as being 0 "zero"

  • 1

    Sorry it was a typo I ended up writing dataInicios.getTime() on the date of the hearing also, rather than dataFinal.getTime(). Corrected

1

Hail!

That solves?

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

Exit:

Quantidade de dias: 28

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.