3
I currently use this code:
// Pega a hora
Date hora = new Date();
hora.getTime();
He returns it to me:
Thu Aug 28 21:55:42 BRT 2014
I would like a way to get only hour, minute and second. How to do this?
3
I currently use this code:
// Pega a hora
Date hora = new Date();
hora.getTime();
He returns it to me:
Thu Aug 28 21:55:42 BRT 2014
I would like a way to get only hour, minute and second. How to do this?
9
A very simple way is to use a SimpleDateFormat
:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date hora = Calendar.getInstance().getTime(); // Ou qualquer outra forma que tem
String dataFormatada = sdf.format(hora);
As this using the Android tag, recommend the documentation of the Android platform for the SimpleDateFormat
that has some details that the Java platform may not have.
If you need to work with the device locale for date, take a look at the documentation that will help you.
As alerted, when using Simpledateformat on Android, the lint
recommends using the Locale
.
One of the solutions would be:
// Isso era buscar o locale do dispositivo
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
Other forms are:
SimpleDateFormat.getTimeInstance()
// ou
SimpleDateFormat.getTimeInstace(int style);
// ou
SimpleDateFormat.getTimeInstance(int style, Locale locale);
The attribute style
can assume one of the values:
DateFormat.FULL
DateFormat.LONG
DateFormat.MEDIUM
DateFormat.SHORT
DateFormat.DEFAULT
Just as an example of formatting, the result for each style is:
DateFormat.FULL -> 10:46:28 PM Brasilia Standard Time
DateFormat.LONG -> 10:46:28 PM GMT-03:00
DateFormat.MEDIUM -> 10:46:28 PM
DateFormat.SHORT -> 10:46 PM
DateFormat.DEFAULT -> 10:46:28 PM
0
Try:
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Brazil/East"));
int ano = calendar.get(Calendar.YEAR);
int mes = calendar.get(Calendar.MONTH); // O mês vai de 0 a 11.
int semana = calendar.get(Calendar.WEEK_OF_MONTH);
int dia = calendar.get(Calendar.DAY_OF_MONTH);
int hora = calendar.get(Calendar.HOUR_OF_DAY);
int minuto = calendar.get(Calendar.MINUTE);
int segundo = calendar.get(Calendar.SECOND);
Browser other questions tagged java android
You are not signed in. Login or sign up in order to post.
that’s what I was looking for :), only when using the SDF, I got a warning:
– JBarbosa
"To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new Simpledateformat(String template, Locale locale) with for example Locale.US for ASCII Dates."
– JBarbosa
@Josinaldo, thanks for the warning, had not attacked me to this in the documentation. I will include this and the difference of
styles
.– Wakim
Solved: ("HH:mm:ss", Locale.getDefault())
– JBarbosa
@Josinaldo, include the result using the
styles
, I believe it does not meet your requirement, but it is only to exemplify the use of them.– Wakim
better yet, I will need in the future :)
– JBarbosa