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:
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.
by efficient, mean want avoid temporary object
string
creation usedmethod 1
, meanwhilemethod 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
Post a Comment