Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing time from a Date object?

I want to remove time from 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'm converting this date (which is in String format) it is appending time also.

I don't want time at all. What I want is simply "21/03/2012".

like image 513
Laxman Rana Avatar asked Mar 22 '12 04:03

Laxman Rana


People also ask

How do I remove the time from a Date object?

Use the toDateString() method to remove the time from a date, e.g. new Date(date. toDateString()) . The method returns only the date portion of a Date object, so passing the result to the Date() constructor would remove the time from the date. Copied!

How do I remove T and Z from timestamp?

To remove the T and Z characters from an ISO date in JavaScript, we first need to have the date in ISO format. If you don't have it already, you can convert it to ISO using the toISOString function on a Date object. This will get rid of both T and Z, resulting in "2022-06-22 07:54:52.657".


1 Answers

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();     }  } 
like image 99
Sunil Manheri Avatar answered Oct 03 '22 01:10

Sunil Manheri