I have a method that receives a LocalTime object, and that returns a String , the objective of this method is that when receiving a time it is for example --> 13:00 returns("It is 13 o'clock") , another example would be - -> 17:08("It is 17 and 8"). Where should I start? Passing the LocalTime object to a String?
EDIT I think I've already got the answer, I'm posting it in case someone wants to know.
private static String generaTextoHora(LocalTime tiempo){
Integer hora = tiempo.getHour();
Integer minutos = tiempo.getMinute();
String mensaje = "";
if(minutos==00){
mensaje = "son las " + hora + " en punto";
}else if(minutos>0 && minutos <15){
mensaje = "son las " + hora + " y " + minutos;
}else if(minutos==15){
mensaje = "son las " + hora +" y cuarto";
}else if(minutos>15 && minutos>30){
mensaje = "son las " + hora + " y " + minutos;
}else if(minutos==30){
mensaje = "son las" + hora + " y media";
}else if(minutos>30 && minutos<=59){
mensaje = "son las" + hora + " y " + minutos;
}
return mensaje;
}
As always, refer to the documentation for the class you want to use.
LocalTime
There you will see methods like
getHour()
,getMinute()
, orgetSecond()
You will then be able to return a String based on those fields. Something like:
A String should never be equalized using == since it is an object .equals() should be used to compare. @lois6b
Use the toString() method which will convert it to a string like:
- "HH:MM"
- "HH:MM:SS"
- ...
- "HH:MM:SS.999999999"
Then you can separate with a split(:");