Removing time from a Date object?

前端 未结 19 1207
余生分开走
余生分开走 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:55

    Apache Commons DateUtils has a "truncate" method that I just used to do this and I think it will meet your needs. It's really easy to use:

    DateUtils.truncate(dateYouWantToTruncate, Calendar.DAY_OF_MONTH);
    

    DateUtils also has a host of other cool utilities like "isSameDay()" and the like. Check it out it! It might make things easier for you.

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

    A bit of a fudge but you could use java.sql.Date. This only stored the date part and zero based time (midnight)

    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, 2011);
    c.set(Calendar.MONTH, 11);
    c.set(Calendar.DATE, 5);
    java.sql.Date d = new java.sql.Date(c.getTimeInMillis());
    System.out.println("date is  " + d);
    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    System.out.println("formatted date is  " + df.format(d));
    

    gives

    date is  2011-12-05
    formatted date is  05/12/2011
    

    Or it might be worth creating your own date object which just contains dates and not times. This could wrap java.util.Date and ignore the time parts of it.

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

    You can write that for example:

    private Date TruncarFecha(Date fechaParametro) throws ParseException {
        String fecha="";
        DateFormat outputFormatter = new SimpleDateFormat("MM/dd/yyyy");
        fecha =outputFormatter.format(fechaParametro);
        return outputFormatter.parse(fecha);
    }
    
    0 讨论(0)
  • 2020-12-15 03:01

    Don't try to make it hard just follow a simple way

    date is a string where your date is saved

    String s2=date.substring(0,date.length()-11);
    

    now print the value of s2. it will reduce your string length and you will get only date part.

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

    Can't believe no one offered this shitty answer with all the rest of them. It's been deprecated for decades.

    @SuppressWarnings("deprecation")
    ...
        Date hitDate = new Date();
        hitDate.setHours(0);
        hitDate.setMinutes(0);
        hitDate.setSeconds(0);
    
    0 讨论(0)
  • 2020-12-15 03:02

    you could try something like this:

    import java.text.*;
    import java.util.*;
    public class DtTime {
    public static void main(String args[]) {
    String s;
    Format formatter;
      Date date = new Date();
      formatter = new SimpleDateFormat("dd/MM/yyyy");
      s = formatter.format(date);
      System.out.println(s);
        }
    }
    

    This will give you output as21/03/2012

    Or you could try this if you want the output as 21 Mar, 2012

    import java.text.*;
    import java.util.*;
    public class DtTime {
    public static void main(String args[]) {
        Date date=new Date();
    String df=DateFormat.getDateInstance().format(date);
    System.out.println(df);
        }
    }
    
    0 讨论(0)
提交回复
热议问题