Simpleformatdata returning null HH:mm:ss

Asked

Viewed 122 times

1

I need to take only the time informed by the user, but the method I did returns null when I try to change the format to HH:mm:ss

public static Date formataData(String data) throws Exception {
    if (data == null || data.equals(""))
        return null;
    Date date = null;
    try {
        DateFormat formatter = new SimpleDateFormat("HH:mm");
        date = (java.util.Date)formatter.parse(data);
    } catch (ParseException e) {
        throw e;
    }
    return date;
}

Mainactivity.java:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText peso = (EditText) findViewById(R.id.pesotxt);
    final EditText iniciosono = (EditText) findViewById(R.id.iniciohorario);
    final EditText fimsono = (EditText) findViewById(R.id.fimhorario);


                    Date hInicioSono = null;
                    Date hFimSono = null;
                    try {
                        hInicioSono = formataData(iniciosono.getText().toString());
                        hFimSono = formataData(fimsono.getText().toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

When I call the method:

    hInicioSono = formataData(iniciosono.getText().toString());

What happens:

inserir a descrição da imagem aqui

I wanted the date to come out 23:00, but get out "Thu Jan 01 23:00:00 BRT 1970".
I need to check if the current time is between inicioSono and FimSono but never will be because the date always comes 1970.

  • 2

    What is being passed to this method? If null is being returned it is because either an empty or null string is being passed. See here was no problem: https://ideone.com/LJyVCv

  • @Florida not necessarily, see the link I posted, I didn’t change any of her code and it worked normally. The problem there is the input, the method itself has no apparent problem.

  • 1

    I already saw it, so I deleted it. = / It would be good if he put the line used to call this function.

  • I already debugged and the string n is null, when it reaches the line "date = (java.util.Date)Formatter.parse(data);" it passes straight through

  • 1

    What is iniciosono? Add a [mcve] It is difficult to analyze individual pieces of code.

1 answer

1


One of the ways is to return a String even, since the parse does not problem:

public static String formataData(String data) throws Exception {
    if (data == null || data.equals(""))
        return null;
    Date date = null;
    DateFormat formatter;
    try {
        formatter = new SimpleDateFormat("HH:mm:ss");
        date = (java.util.Date) formatter.parse(data);
    } catch (ParseException e) {
        throw e;
    }
    return formatter.format(date);
}

Functioning in the IDEONE

Or since you just need to compare time, without taking into account the date, you can use the class Localtime package java.time java 8:

public static LocalTime formataData(String data) throws Exception {
    if (data == null || data.equals(""))
        return null;
    LocalTime lt = LocalTime.parse(data);
    return lt;
}

See also working on IDEONE.

To use the latter method, you would also need to change the type of your variables hInicioSono and hFimSono:

LocalTime hInicioSono;
LocalTime hFimSono;
  • It worked! I used the return string method

Browser other questions tagged

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