I am a beginner with programming, and using Java. I'm trying to create a function so that the current date will be displayed as a String
and have no timestamp information. I want the format of the date to be "yyyy/MM/dd" but I am having trouble. I have searched and have seen that java.util.Calendar
can be used, and it is possible to clear the field values that go with the timestamp, such as MINUTE
, SECOND
etc. set to a value of 0. I tried this code but am having issues:
package com.java.date.util
import java.util.Calendar;
import java.util.Date;
public class DateWithNoTimestamp {
public final static String dateOnly(Date date) {
String dateNoTime = date.toString();
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 dateNoTime;
}
}
I am a beginning programming and I am just trying to understand the basics at the moment. I know there's a way to show the format of the date I'm just unsure of how to go about it.