0

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.

Edd
  • 3,724
  • 3
  • 26
  • 33
wheelerlc64
  • 435
  • 2
  • 5
  • 17

3 Answers3

3

Use a SimpleDateFormat - it's just one line:

return new SimpleDateFormat("yyyyMMdd").format(new Date());

If you want a different format, use dd/MM/yyyy etc instead.

Note that new Date() is the current date, so no need to pass a date in to your method if you want the current date.

Also, avoid using Calendar class: It's a bit broken.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You can get an instance of the current date/time by using new Date(). You can then use a SimpleDateFormatter with the format you provided to get only the day/month/year.

Eric Stein
  • 13,209
  • 3
  • 37
  • 52
0

Format the date like this: (this works for me)

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(calendar.getTime()); // it gives "Sab May 17 10:22:23 EST 2014"
String formatted = myFormat.format(calendar.getTime());
System.out.println(formatted);         // it gives "2014-05-17"

Cheers!

yeaaaahhhh..hamf hamf
  • 746
  • 2
  • 13
  • 34
  • the best thing for me would be to wrap all of this in one method, then be able to call the method in main. This method will be called from another class, and this method and it's attributes and variables will be in another class. Any way to do that would be nice. Sorry it is an assignment I've been working on and just going by directions of the assignment. – wheelerlc64 Mar 13 '14 at 15:19
  • create a new class, and wrap the code with public static String myCalFunction(){....} which will return a String as a result. You may create a new instance of this clas in your main and then call the function you want. If you put STATIC you can use it directly: MyCalClass.myCalFunction(); – yeaaaahhhh..hamf hamf Mar 14 '14 at 14:54