Slender Man
Slender Man

Reputation: 35

Displaying date in a double digit format - java

Instead for example "9/1/1996" I want my code to display "09/01/1996". I don't know how to describe my question more specific. Here is my code for the MyDate class:

public class MyDate {

    private int year;
    private int month;
    private int day;

    public MyDate(int y, int m, int d){
        year = y;
        month = m;
        day = d;

        System.out.printf("I'm born at: %s\n", this);
    }

    public  String toString(){
        return String.format("%d/%d/%d", year, month,  day);
    }
  }

And here is my main method:

public class MyDateTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        MyDate birthday = new MyDate(31,12,1995);
    }

}

p.s I know that there is a way with importing calendar, but I rather doing it this way.

Upvotes: 4

Views: 3850

Answers (5)

Channa Jayamuni
Channa Jayamuni

Reputation: 1905

According to your problem it seems you are using customized class for date. But you can use inbuilt java.util.Date class for any kind of Date represent or compare. So you can use DateFormat to display formatted date time. Consider the following example.

    Date date = //code to get your date

    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    System.out.println(dateFormat.format(date));

OUTPUT:

13/10/2015

Additionally you there you should specify the pattern for your DateFormat. Use following table for date formatting characters.

enter image description here

If you are still need to use your magic MyDate class

You can use System.out.printf specified with leading zeros as follows.

System.out.printf("%02d/%02d/%04d", date, month, year);

Upvotes: 3

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31299

This is a number formatting question. Before the 'd' in %d, you can put the length of the number field, and if you want leading zeros instead of leading spaces, then you add a '0' in front of that. So a 2-digit number field with leading zeros is %02d.

So your code becomes:

public String toString() {
    return String.format("%04d/%02d/%02d", year, month, day);
}

Upvotes: 2

Hunter McMillen
Hunter McMillen

Reputation: 61520

You can change your format string for String.format to pad with leading zeros:

return String.format("%d/%02d/%02d", year, month,  day);
  • 0 means pad the output with leading zeros
  • 2 means the output should be 2 characters wide (day and month)
  • year should not need a modifier since it will always be 4 digits (at least for the next 8 millennia)

ideone example

Upvotes: 1

chareon
chareon

Reputation: 120

you should import also this import java.text.SimpleDateFormat; import java.text.DateFormat; when using

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    System.out.println(dateFormat.format(date)); 

Upvotes: 1

chengpohi
chengpohi

Reputation: 14227

String.format("%d/%02d/%02d", year, month,  day)

String format the output.

Upvotes: 1

Related Questions