Sunday, 18 March 2018

android - Add 0 to Month: i.e. "1" -> "01"




I'm doing the conversion of the month, the format (LONG) to an integer. But the months are converted without the 0. I must add the 0 of the months lower 10. How can I do?




    String month = monthSelector.getSelectedItem().toString();
Calendar cal = Calendar.getInstance();
try {
cal.setTime(new SimpleDateFormat("MMMM").parse(month));
} catch (ParseException e) {

e.printStackTrace();
}
int monthInt = cal.get(Calendar.MONTH) + 1;


Answer



This is what I do:



int monthInt = cal.get(Calendar.MONTH) + 1; 
String monthConverted = ""+monthInt;
if(monthInt<10){
monthConverted = "0"+monthConverted;
}


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...