aspose file tools*
The moose likes Beginning Java and the fly likes How do i print 1 as 01? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do i print 1 as 01?" Watch "How do i print 1 as 01?" New topic
Author

How do i print 1 as 01?

Suresh Ramanan
Greenhorn

Joined: Feb 08, 2009
Posts: 27
Hi!

I'm working with dates.
At this point, the design is pretty crude and i'm just making an int 01 and incrementing it for the day.

so i wish to print out the dates in YYYY/MM/DD format.
so i wish to get the date as, say, 2011/02/01
but i'm getting it as 2011/02/1

"02" for month is a string, so no issue there
How do i make sure that 01 is displayed as 01 without getting trimmed as 1?

Best wishes,
Suresh


SCJP 5.0 SCWCD 1.5 SCWCD Hall of Fame
vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 76



I think this will help you
Wim Vanni
Ranch Hand

Joined: Apr 06, 2011
Posts: 96

Use a Formatter.

Edit: and in case of a data a date formatter of course Sorry, jumped the gun here

Cheers,
Wim
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3370
    
    9
Hi Suresh, take a look at the DateFormat class.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

What are you using to format this date? SimpleDateFormat, or something else?


vinayak jog, there are two things wrong with your code:
1) mm does not print out the month but the minute.
2) format will only throw a NullPointerException so you shouldn't surround it with a try-catch block. It's completely unnecessary. It's the parse method that can throw a ParseException.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Suresh Ramanan
Greenhorn

Joined: Feb 08, 2009
Posts: 27
hey thank you guys!!
am gonna try them all out!

Suresh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

vinayak jog wrote:

I think this will help you

This contains a bug. You must use MM, not mm, for months. mm means minutes, not months.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
deca leni
Ranch Hand

Joined: Jun 18, 2011
Posts: 49

look at the DateFormat class in java API doc


Do or die...?? No, Do before you die... || (SCJP .6)
vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 76

Jesper de Jong wrote:
vinayak jog wrote:

I think this will help you

This contains a bug. You must use MM, not mm, for months. mm means minutes, not months.


thanks jasper that was a typo. i was in hurry
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do i print 1 as 01?
 
Similar Threads
Java Calendar Dates
Extracting and Comparing Data
how to convert gregoriancalendar obj to sqldate?
Getting Value from Dynamiclly Created TextBoxes
Java Calendar Dates