Removing time from a Date object?

前端 未结 19 1257
余生分开走
余生分开走 2020-12-15 02:26

I want to remove time from Date object.

DateFormat df;
String date;
df = new SimpleDateFormat(\"dd/MM/yyyy\");
d = eventList.get(0).getStartDate         


        
19条回答
  •  时光取名叫无心
    2020-12-15 03:03

    What you want is impossible.

    A Date object represents an "absolute" moment in time. You cannot "remove the time part" from it. When you print a Date object directly with System.out.println(date), it will always be formatted in a default format that includes the time. There is nothing you can do to change that.

    Instead of somehow trying to use class Date for something that it was not designed for, you should look for another solution. For example, use SimpleDateFormat to format the date in whatever format you want.

    The Java date and calendar APIs are unfortunately not the most well-designed classes of the standard Java API. There's a library called Joda-Time which has a much better and more powerful API.

    Joda-Time has a number of special classes to support dates, times, periods, durations, etc. If you want to work with just a date without a time, then Joda-Time's LocalDate class would be what you'd use.

提交回复
热议问题