Removing time from a Date object?

前端 未结 19 1206
余生分开走
余生分开走 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:02
    String substring(int startIndex, int endIndex)
    

    In other words you know your string will be 10 characers long so you would do:

    FinalDate = date.substring(0,9);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-15 03:04

    If you are using Java 8+, use java.time.LocalDate type instead.

    LocalDate now = LocalDate.now();
    System.out.println(now.toString());
    

    The output:

    2019-05-30
    

    https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html

    0 讨论(0)
  • 2020-12-15 03:07
    Date dateWithoutTime =
        new Date(myDate.getYear(),myDate.getMonth(),myDate.getDate()) 
    

    This is deprecated, but the fastest way to do it.

    0 讨论(0)
  • 2020-12-15 03:08

    The correct class to use for a date without time of day is LocalDate. LocalDate is a part of java.time, the modern Java date and time API.

    So the best thing you can do is if you can modify the getStartDate method you are using to return a LocalDate:

        DateTimeFormatter dateFormatter = DateTimeFormatter
                .ofLocalizedDate(FormatStyle.SHORT)
                .withLocale(Locale.forLanguageTag("en-IE"));
    
        LocalDate d = eventList.get(0).getStartDate(); // We’re now getting a LocalDate using this method
        String dateString = d.format(dateFormatter);
        System.out.println(dateString);
    

    Example output:

    21/03/2012

    If you cannot change the getStartDate, you may still be able to add a new method returning the type that we want. However, if you cannot afford to do that just now, convert the old-fashioned Date that you get (I assume java.util.Date):

        d = eventList.get(0).getStartDate(); // I'm getting the old-fashioned Date using this method
        LocalDate dateWithoutTime = d.toInstant()
                .atZone(ZoneId.of("Asia/Kolkata"))
                .toLocalDate();
    

    Please insert the time zone that was assumed for the Date. You may use ZoneId.systemDefault() for the JVM’s time zone setting, only this setting can be changed at any time from other parts of your program or other programs running in the same JVM.

    The java.util.Date class was what we were all using when this question was asked 6 years ago (no, not all; I was, and we were many). java.time came out a couple of years later and has replaced the old Date, Calendar, SimpleDateFormat and DateFormat. Recognizing that they were poorly designed. Furthermore, a Date despite its name cannot represent a date. It’s a point in time. What the other answers do is they round down the time to the start of the day (“midnight”) in the JVM’s default time zone. It doesn’t remove the time of day, only sets it, typically to 00:00. Change your default time zone — as I said, even another program running in the same JVM may do that at any time without notice — and everything will break (often).

    Link: Oracle tutorial: Date Time explaining how to use java.time.

    0 讨论(0)
  • 2020-12-15 03:09

    You can remove the time part from java.util.Date by setting the hour, minute, second and millisecond values to zero.

    import java.util.Calendar;
    import java.util.Date;
    
    public class DateUtil {
    
        public static Date removeTime(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            return cal.getTime();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题