• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Format Date error

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

this is my code:




and this is my error:


Any help , please?

Thanks

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the String#format method or a Formatter object as an alternative.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic