Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i print 1 as 01?

 
Suresh Ramanan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I think this will help you
 
Wim Vanni
Ranch Hand
Posts: 96
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5553
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suresh, take a look at the DateFormat class.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Suresh Ramanan
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thank you guys!!
am gonna try them all out!

Suresh
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
deca leni
Ranch Hand
Posts: 49
Java Java ME PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look at the DateFormat class in java API doc
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic