• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to test if java.util.date object has 00:00:00 as hours,minutes,seconds  RSS feed

 
Shazia Bashir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i want to return formattedDate without hours,minutes,seconds if they are 00:00:00 in date-object, how to do this ?:

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can think of two ways:

1) new SimpleDateFormat("HH:MM:SS") and see whether everything is a zero
2) Create a Calendar object and setTime() on it to your date. Then call cal.get(Calendar.HOUR) == 0 and the like.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shazia Bashir wrote:want to return formattedDate without hours,minutes,seconds if they are 00:00:00 in date-object, how to do this ?:

Kind of on the same lines as Jeanne's 1st suggestion:
Format it with your 2nd format string and see if the String ends with " 00:00:00". If it does, return its prefix; otherwise, return the whole String.

Winston

PS: new SimpleDateFormat("DD-MM-YYYY HH:MM:SS").format(date) is not the best way to do that, because it creates a new SimpleDateFormat object every time. Set the format up separately (maybe in a static field) and just use it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!