• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Format Date error

 
Isaac Ferguson
Ranch Hand
Posts: 846
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

this is my code:




and this is my error:


Any help , please?

Thanks

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Isaac Ferguson
Ranch Hand
Posts: 846
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 846
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 846
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10430
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the String#format method or a Formatter object as an alternative.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic