Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calendar giving HOUR and MINUTE without leading zero - Android

I know a lot of tutorials there about calendar giving minute and hours without leading zero. But those doesnt work for my codes. Can someone help me how to do this base on my codes? Calendar.MINUTE is not showing the zero integer whenever the minite is from 0-9.

Calendar cal = GregorianCalendar.getInstance();
  String am_pm, hour;
  cal.add(Calendar.MONTH, +1);

  if(cal.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
      else
      am_pm = "PM"; 

tv.setText(cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.YEAR));
  tv2.setText(cal.get(Calendar.HOUR) + ":" + cal.get(Calendar.MINUTE) + " " + am_pm);
tv3.setText("Offered");
like image 270
jajaja Avatar asked Mar 20 '14 07:03

jajaja


1 Answers

you can use DateFormat or Simple way to do this is

String month=String.format("%02d",cal.get(Calendar.MONTH));
String day=String.format("%02d",cal.get(Calendar.DAY_OF_MONTH));
String year=String.format("%02d",cal.get(Calendar.YEAR));
tv.setText( month+ "-" + day + "-" +year );
like image 178
Ketan Ahir Avatar answered Oct 19 '22 14:10

Ketan Ahir