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:
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.
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
– user28595
@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.
– user28595
I already saw it, so I deleted it. = / It would be good if he put the line used to call this function.
– Florida
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
– Éowyn
What is
iniciosono
? Add a [mcve] It is difficult to analyze individual pieces of code.– user28595