aspose file tools*
The moose likes Beginning Java and the fly likes Changing the date format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Changing the date format" Watch "Changing the date format" New topic
Author

Changing the date format

Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
hey everyone,

I have a method that gets todays date, but I can't get it to change the format. right now it is Friday 4 May 2007 but I want it to display like Friday 04 May 2007.
here is my code:


I tried the SimpleDateFormat but I can't seem to get it to work, any ideas?

davy


How simple does it have to be???
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3658
    
  15
SimpleDateFormat is the way to go. Show us what you tried and what you mean by 'it didn't work'. Could you not get it to compile or did it not give you the output you expected ?


Joanne
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384


i tried:


it compiles but I get a runtime error:


davy
[ May 04, 2007: Message edited by: Davy Kelly ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3658
    
  15
There's nothing obvious in that code that would produce a NPE. Which is line 108 ?
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
today = sdfbs.format(caltoday.getTime()); //line 108

davy
[ May 04, 2007: Message edited by: Davy Kelly ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3658
    
  15
Originally posted by Davy Kelly:
today = sdfbs.format(caltoday.getTime()); //line 108


For that line to throw an NPE, either sdfbs or caltoday must be null. Try putting in some print statements or running it thru a debugger to see which it is.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Thanks Joanne,

I changed my method, which now works. I had the SimpleDateFormat as a class variable (this was null), so I put it into the method. here it is in full which works.


davy
[ May 04, 2007: Message edited by: Davy Kelly ]
 
wood burning stoves
 
subject: Changing the date format