wood burning stoves 2.0
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
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: 81



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: 3989
    
  18

Hi Suresh, take a look at the DateFormat class.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20079
    
  30

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 - OCEJPAD 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: 14957
    
  26

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 8 API documentation
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: 81

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
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: How do i print 1 as 01?
 
It's not a secret anymore!