File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Format Date error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Format Date error" Watch "Format Date error" New topic
Author

Format Date error

Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 351
Hi,

this is my code:




and this is my error:


Any help , please?

Thanks

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

The object that you pass to formatter.format(...) has to be a java.util.Date object. You are passing it a java.util.Calendar instead. Use someDate.getTime() in line 4 instead of someDate.


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

Joined: Jun 22, 2012
Posts: 351
Hi,

thanks it works. But I need know the date of the last week I mean, could I get something like date-7 days? Thanks

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Angus Ferguson wrote:Hi,

thanks it works. But I need know the date of the last week I mean, could I get something like date-7 days? Thanks



Calendar has an add() method, so add -7 to the DATE field.
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 351
But if I use

The object that you pass to formatter.format(...) has to be a java.util.Date object. You are passing it a java.util.Calendar instead. Use someDate.getTime() in line 4 instead of someDate.

I can't use:

Calendar has an add() method, so add -7 to the DATE field.

My code now is like this:



Any idea?

Thanks
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

It is possible to convert back and forth between Calendar and Date. So when you need to do your calculations, use a Calendar, and when you need to format for output, convert the Calendar into a Date.
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 351
Hi,

when I use this code:



In someDate.getDate(); I get 1970-01-01 01:00:00.

I am looking for the last week

Any idea?

Thanks

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7772
    
  21

Angus Ferguson wrote:when I use this code:
...
In someDate.getDate(); I get 1970-01-01 01:00:00.

I am looking for the last week
Any idea?

Well first, you really should read the API documentation for java.util.Date and java.util.Calendar, because there's a lot to know.

For example:
What do you think Calendar.DAY_OF_YEAR - 7 returns? And what unit is it going to be in?
What does Date.setDate() take?

Until you understand what these methods do, you'll simply be floundering around in the dark; and that's no way to program.

Winston

PS: It's probably worth mentioning that Date.setDate() is deprecated, which you'd also know if you had read the documentation.


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
Consider the String#format method or a Formatter object as an alternative.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Format Date error