• Post Reply Bookmark Topic Watch Topic
  • New Topic

get()ing Calendar.MONTH with two digits

 
Michael Brewer
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how I can use get() or one of its related methods to get the month of a Calendar instance as a two digit number? It's currently giving me 2 instead of 02. Below is the code I'm using.
 
Junilu Lacar
Marshal
Posts: 10409
125
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really a formatting issue. get() returns an int, plain and simple. If you want to format a single digit number with a leading zero, you write something as simple as:
System.out.println(((x < 10) ? "0" : "") + x);
or as complicated as
System.out.println(java.text.MessageFormat.format("{0,number,#00}", new Object[] {new Integer(x)}));
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!