Removing time from a Date object?

前端 未结 19 1204
余生分开走
余生分开走 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 02:43

    java.util.Date represents a date/time down to milliseconds. You don't have an option but to include a time with it. You could try zeroing out the time, but then timezones and daylight savings will come into play--and that can screw things up down the line (e.g. 21/03/2012 0:00 GMT is 20/03/2012 PDT).

    What you might want is a java.sql.Date to represent only the date portion (though internally it still uses ms).

    0 讨论(0)
  • 2020-12-15 02:45

    What about this:

        Date today = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    
        today = sdf.parse(sdf.format(today));
    
    0 讨论(0)
  • 2020-12-15 02:47

    May be the below code may help people who are looking for zeroHour of the day :

        Date todayDate = new Date();
        GregorianCalendar todayDate_G = new GregorianCalendar();
        gcd.setTime(currentDate);
        int _Day    = todayDate_GC.get(GregorianCalendar.DAY_OF_MONTH);
        int _Month  = todayDate_GC.get(GregorianCalendar.MONTH);
        int _Year   = todayDate_GC.get(GregorianCalendar.YEAR);
    
        GregorianCalendar newDate = new GregorianCalendar(_Year,_Month,_Day,0,0,0);
        zeroHourDate = newDate.getTime();
        long zeroHourDateTime = newDate.getTimeInMillis();
    

    Hope this will be helpful.

    0 讨论(0)
  • 2020-12-15 02:51

    The quick answer is :

    No, you are not allowed to do that. Because that is what Date use for.

    From javadoc of Date :

    The class Date represents a specific instant in time, with millisecond precision.

    However, since this class is simply a data object. It dose not care about how we describe it. When we see a date 2012/01/01 12:05:10.321, we can say it is 2012/01/01, this is what you need. There are many ways to do this.

    Example 1 : by manipulating string

    Input string : 2012/01/20 12:05:10.321

    Desired output string : 2012/01/20

    Since the yyyy/MM/dd are exactly what we need, we can simply manipulate the string to get the result.

    String input = "2012/01/20 12:05:10.321";
    String output = input.substring(0, 10);  // Output : 2012/01/20
    

    Example 2 : by SimpleDateFormat

    Input string : 2012/01/20 12:05:10.321

    Desired output string : 01/20/2012

    In this case we want a different format.

    String input = "2012/01/20 12:05:10.321";
    DateFormat inputFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
    Date date = inputFormatter.parse(input);
    
    DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
    String output = outputFormatter.format(date); // Output : 01/20/2012
    

    For usage of SimpleDateFormat, check SimpleDateFormat JavaDoc.

    0 讨论(0)
  • 2020-12-15 02:51

    You can also manually change the time part of date and format in "dd/mm/yyyy" pattern according to your requirement.

      public static Date getZeroTimeDate(Date changeDate){
    
            Date returnDate=new Date(changeDate.getTime()-(24*60*60*1000));
            return returnDate;
        }
    

    If the return value is not working then check for the context parameter in web.xml. eg.

       <context-param> 
            <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
            <param-value>true</param-value>
        </context-param>
    
    0 讨论(0)
  • 2020-12-15 02:51

    In addtition to what @jseals has already said. I think the org.apache.commons.lang.time.DateUtils class is probably what you should be looking at.

    It's method : truncate(Date date,int field) worked very well for me.

    JavaDocs : https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/time/DateUtils.html#truncate(java.util.Date, int)

    Since you needed to truncate all the time fields you can use :

    DateUtils.truncate(new Date(),Calendar.DAY_OF_MONTH)
    
    0 讨论(0)
提交回复
热议问题