How to get the current date correctly?

advertisements

This question already has an answer here:

  • incorrect conversion of String into date format 3 answers

I tried to get the current date in a specific format, but instead of the month got minutes

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CurrentDate {

    public static String currentDate() {
    String timeStamp = new SimpleDateFormat("mm/dd/yyyy").format(Calendar.getInstance().getTime());
    return timeStamp;
    }

    public static void main (String [] args) {
        System.out.println(currentDate());
    }

}

Why this code gives minutes ... i tried mon ... ???


You want "MM/dd/yyyy" in the SimpleDateFormat. If you look at the documentation at http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html you'll see that lower case m always means minutes, and capital M is month.