This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes get()ing Calendar.MONTH with two digits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "get()ing Calendar.MONTH with two digits" Watch "get()ing Calendar.MONTH with two digits" New topic
Author

get()ing Calendar.MONTH with two digits

Michael Brewer
Ranch Hand

Joined: Jun 27, 2002
Posts: 54
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
Bartender

Joined: Feb 26, 2001
Posts: 4458
    
    6

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)}));


Junilu - [How to Ask Questions] [How to Answer Questions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: get()ing Calendar.MONTH with two digits