How do I get a Date without time in Java? -


continuing stack overflow question java program current date without timestamp:

what efficient way date object without time? there other way these two?

// method 1 simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); date datewithouttime = sdf.parse(sdf.format(new date()));  // method 2 calendar cal = calendar.getinstance(); cal.set(calendar.hour_of_day, 0); cal.set(calendar.minute, 0); cal.set(calendar.second, 0); cal.set(calendar.millisecond, 0); datewithouttime = cal.gettime(); 

update:

  1. i knew joda-time; trying avoid additional library such simple (i think) task. based on answers far joda-time seems extremely popular, might consider it.

  2. by efficient, mean want avoid temporary object string creation used method 1, meanwhile method 2 seems hack instead of solution.

do absolutely have use java.util.date? thoroughly recommend use joda time or java.time package java 8 instead. in particular, while date , calendar always represent particular instant in time, no such concept "just date", joda time does have type representing (localdate). code clearer if you're able use types represent you're trying do.

there many, many other reasons use joda time or java.time instead of built-in java.util types - they're far better apis. can convert to/from java.util.date @ boundaries of own code if need to, e.g. database interaction.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -