• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 76888
366
  • 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.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic