Hi all I am using below code to get android phone time, but it is giving me minutes without zero if the minutes are in between 1 to 9.
for example:right now I have time on my device 12:09 but its giving me as 12:9
Calendar c = Calendar.getInstance();
int hrs = c.get(Calendar.HOUR);
int mnts = c.get(Calendar.MINUTE);
String curTime = "" + hrs + ":" + mnts;
return curTime;
after above code I also try below code its giving same thing as above, minutes without zero before number it the minutes in between 1 to 9 . .
final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
Date date = cal.getTime();
int mHour = date.getHours();
int mMinute = date.getMinutes();
As Egor said, an int
is just an integer. Integers don't have leading zeros. They can only be displayed with leading zeros when you convert them to String
objects. One way to do that is like this:
String curTime = String.format("%02d:%02d", hrs, mnts);
The format string %02d
formats an integer with leading zeros (that's the 0
in %02d
), always taking up 2 digits of width (that's the 2
in %02d
).
That would produce the String
12:09
All is said about integer. But dealing with dates and Calendars to display that information should be used like so:
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yourpattern"); //like "HH:mm" or just "mm", whatever you want
String stringRepresentation = sdf.format(date);
the pattern "mm" will have a leading zero if it is between 0 and 9. If you use "mmmm" you'll get 0009, which doesn't look like it makes a lot of sense, but it all depends on what you want. :)
if you use pattern HH:mm
you'll get 12:09
(the current time of your date instance).
You can format date using SimpleDateFormat class
if you are getting date and time from Calendar instance:-
final Calendar c = Calendar.getInstance();
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
timeEdittext.setText(timeFormat.format(c.getTime()));
dateEdittext.setText(dateFormat.format(c.getTime()));
if you have day, month, year, hour, minute as integer(usually happens in datepicker and timepicker dialogs)
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, dayOfMonth);
String date = new SimpleDateFormat("dd-MM-yyyy").format(calendar.getTime());
Used below method to convert Hour:Minute
in double format.
/*
* make double format hour:minute string
* @hour : 1
* @minute : 2
* return : 01:02
* */
public static String hourMinuteZero(int hour,int mnts){
String hourZero = (hour >=10)? Integer.toString(hour):String.format("0%s",Integer.toString(hour));
String minuteZero = (mnts >=10)? Integer.toString(mnts):String.format("0%s",Integer.toString(mnts));
return hourZero+":"+minuteZero;
}
function time()
{
var time = new Date();
var hh = time.getHours();
var mmm = time.getMinutes();
if(mmm<10)
{
mmm='0'+mmm
}
time = 'T'+hh+':'+mmm;
return time;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With