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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 331
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: 14074
    
  16

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: 331
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: 331
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: 331
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: 7484
    
  18

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: 37874
    
  22
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
 
Similar Threads
Converting string to specific date format
String to date format
Problem with date formatting
SimpleDateFormat
jsp/tomcat undefined type error