Removing time from a Date object?

I want to remove the time from a Date object.

 DateFormat df; String date; df = new SimpleDateFormat("dd/MM/yyyy"); d = eventList.get(0).getStartDate(); // I'm getting the date using this method date = df.format(d); // Converting date in "dd/MM/yyyy" format 

But when I convert this date (which is in String format), it is also time to add.

I do not want time. I just want to "03/21/2012".

+46
java date datetime
Mar 22 2018-12-12T00:
source share
16 answers

Quick response:

No, you are not allowed to do this. . Date used for this.

From javadoc Date :

The Date class represents a specific point in time accurate to the millisecond.

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

Example 1: string manipulation

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

Desired output line: 2012/01/20

Since yyyy / MM / dd is exactly what we need, we can just 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 line: 2012/01/20 12: 05: 10.321

Desired output line: 01/20/2012

In this case, we need 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 

To use SimpleDateFormat , check out the SimpleDateFormat JavaDoc .

+35
Mar 22 '12 at 5:20
source share
— -

You can remove the time part from java.util.Date by setting the hour, minute, second and millisecond 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(); } } 
+83
Apr 26 2018-12-12T00:
source share

Apocal Commons DateUtils has a truncate method that I just used for this, and I think it will satisfy your needs. It is really easy to use:

 DateUtils.truncate(dateYouWantToTruncate, Calendar.DAY_OF_MONTH); 

DateUtils also contains many other useful utilities, such as "isSameDay ()" and the like. Check this! It can make your life easier.

+22
Apr 25 '13 at 21:46
source share

How about this:

  Date today = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); today = sdf.parse(sdf.format(today)); 
+9
Jan 17 '14 at 8:57
source share

You want impossible .

An object

A Date represents an “absolute” point in time. You cannot “remove part of the time” from it. When you print a Date object directly using System.out.println(date) , it will always be formatted in the default format, which includes time. You can do nothing to change this.

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

The Java Dates and Calendars APIs, unfortunately, are not the most well-designed classes of the standard Java APIs. There is 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 a date without a time, then the Joda-Time LocalDate class will be what you will use.

+6
Mar 22 '12 at 6:13
source share

You can write this, 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); } 
+4
Feb 26 '15 at 17:46
source share

you can 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 a result like 21/03/2012

Or you can try this if you want the result to be 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); } } 
+3
Sep 25 '12 at 18:57
source share
 Date dateWithoutTime = new Date(myDate.getYear(),myDate.getMonth(),myDate.getDate()) 

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

+2
Mar 22 '12 at 6:23
source share

Perhaps the code below can 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.

+2
Mar 06 '15 at 13:50
source share
 String substring(int startIndex, int endIndex) 

In other words, you know that your string will be 10 characters long, so you do:

 FinalDate = date.substring(0,9); 
+1
Mar 22 2018-12-12T00:
source share

java.util.Date represents a date / time up to a millisecond. You have no choice but to turn on time with it. You can try to reset the time, but then time zones and summer time will work - and this can damage things along the line (eg 21/03/2012 0:00 GMT is 20/03/2012 PDT).

You may need java.sql.Date to represent only part of the date (although internally it still uses ms).

+1
Mar 22 2018-12-12T00:
source share

A bit of fiction, but you can use java.sql.Date. This only fixed the date and zero (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 simply contains dates, not times. This can wrap java.util.Date and ignore the temporary parts.

+1
Sep 25 '12 at 20:11
source share

In addition to what @jseals already said. I think the class org.apache.commons.lang.time.DateUtils is probably what you should watch.

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 need to truncate all the time fields you can use:

 DateUtils.truncate(new Date(),Calendar.DAY_OF_MONTH) 
+1
Mar 20 '15 at 17:58
source share

You can also manually change the time part of the date and format in the "dd / mm / yyyy" template to suit your requirements.

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

If the return value does not work, check 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
May 16 '13 at 8:50
source share

Do not try to do it hard, just follow the easy way.

date is the string where your date is stored.

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

now type the value of s2 . this will reduce the length of the string and you will only get the date.

0
Aug 25 '13 at 12:49 on
source share

Another development method here is to use java.sql.Date, since sql Date does not have the time associated with it, whereas java.util.Date always has a timestamp. In this sense, java.sql.Date extends java.util.Date, so the java.util.Date variable can be a reference to java.sql.Date (without time) and to java.util.Date, of course (with a timestamp).

0
Aug 13 '14 at 15:48
source share



All Articles