140

I have a string

String startDate = "06/27/2007";

now i have to get Date object. My DateObject should be the same value as of startDate.

I am doing like this

DateFormat df = new SimpleDateFormat("mm/dd/yyyy");
Date startDate = df.parse(startDate);

But the output is in format

Jan 27 00:06:00 PST 2007.

MikeM
  • 27,227
  • 4
  • 64
  • 80
user755043
  • 1,657
  • 3
  • 12
  • 12
  • 2
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); Date startDate = null; try { startDate = dateFormat.parse("03/04/1975"); String foramtedDate =dateFormat.format(startDate); System.out.println("After format Date is="+foramtedDate); // } catch (ParseException e) { // TODO Auto/generated catch block e.printStackTrace(); } –  Apr 15 '16 at 11:30

5 Answers5

191

You basically effectively converted your date in a string format to a date object. If you print it out at that point, you will get the standard date formatting output. In order to format it after that, you then need to convert it back to a date object with a specified format (already specified previously)

String startDateString = "06/27/2007";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Date startDate;
try {
    startDate = df.parse(startDateString);
    String newDateString = df.format(startDate);
    System.out.println(newDateString);
} catch (ParseException e) {
    e.printStackTrace();
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
citizen conn
  • 15,300
  • 3
  • 58
  • 80
  • 3
    You used the "format" method to show it in required format. But this is String again. Is it possible to have a Date object of the same format as startDateString in your code – user755043 Jun 28 '11 at 17:53
  • 2
    I don't understand the question. The default "format" of a date object is like this Jan 27 00:06:00 PST 2007. If you want to change it you need to use a DateFormat class... Date is just an object, technically it doesnt even have a "format" it just stores data about the date... Overriding the toString method on it might give you a different output, but thats what DateFormat is there for, to maintain integrity and consistency of all your date objects. – citizen conn Jun 28 '11 at 17:58
  • 1
    On another note, if you are going to consistently use this type of format in your application, you could subclass the date object and have a CustomDate that would have helper methods on it to return the type of format you want, and potentially more custom information. – citizen conn Jun 28 '11 at 17:59
  • a date object is a simple Date object of java that has a uniform format of printing itself using toString() @citizen conn's method is one of the correct methods. you can also use methods like getDate() or getMonth(). But avoid them as they are depricated. – mihsathe Jun 28 '11 at 18:00
  • 1
    Your format String for month is wrong. It must be `MM` instead of `mm`. – Buhake Sindi Jul 14 '11 at 07:26
  • He was asking for a date object but you formatted back to string. – viper Jul 28 '16 at 05:32
  • How to convert this format 23 January 2017 - 02:50 pm – srinivas gowda Jan 23 '17 at 09:17
  • 1
    "Cannot find symbol DateFormat." Please include the imports next time... – Mathieu Turcotte Feb 17 '17 at 13:16
  • Good to note that `yyyy` formatting is not strict and will not result in a ParseException if fewer than four digits are input. That is not to say that entering two will be correctly interpreted as an abbreviation, however. Entering 21 will be interpreted as the year 21 not 2021, but no parse exception will be thrown. Seems like the parsing should be more strict in following the pattern passed to the formatter. https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Conner M. Jul 04 '21 at 01:16
20

"mm" means the "minutes" fragment of a date. For the "months" part, use "MM".

So, try to change the code to:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 
Date startDate = df.parse(startDateString);

Edit: A DateFormat object contains a date formatting definition, not a Date object, which contains only the date without concerning about formatting. When talking about formatting, we are talking about create a String representation of a Date in a specific format. See this example:

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    public class DateTest {

        public static void main(String[] args) throws Exception {
            String startDateString = "06/27/2007";

            // This object can interpret strings representing dates in the format MM/dd/yyyy
            DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 

            // Convert from String to Date
            Date startDate = df.parse(startDateString);

            // Print the date, with the default formatting. 
            // Here, the important thing to note is that the parts of the date 
            // were correctly interpreted, such as day, month, year etc.
            System.out.println("Date, with the default formatting: " + startDate);

            // Once converted to a Date object, you can convert 
            // back to a String using any desired format.
            String startDateString1 = df.format(startDate);
            System.out.println("Date in format MM/dd/yyyy: " + startDateString1);

            // Converting to String again, using an alternative format
            DateFormat df2 = new SimpleDateFormat("dd/MM/yyyy"); 
            String startDateString2 = df2.format(startDate);
            System.out.println("Date in format dd/MM/yyyy: " + startDateString2);
        }
    }

Output:

Date, with the default formatting: Wed Jun 27 00:00:00 BRT 2007
Date in format MM/dd/yyyy: 06/27/2007
Date in format dd/MM/yyyy: 27/06/2007
sother
  • 556
  • 4
  • 5
9
    try 
    {  
      String datestr="06/27/2007";
      DateFormat formatter; 
      Date date; 
      formatter = new SimpleDateFormat("MM/dd/yyyy");
      date = (Date)formatter.parse(datestr);  
    } 
    catch (Exception e)
    {}

month is MM, minutes is mm..

mihsathe
  • 8,904
  • 12
  • 38
  • 54
  • 1
    @mihsate if you print the date in your code it would be of format like Mon May 23 00:00:00 PDT 2011. But i need a date object of similar to my string 06/27/2007. – user755043 Jun 28 '11 at 17:50
  • Oh well understand this code is only going to convert your String into a valid java date object. It can not influence the printing format. It becomes just another date object then. – mihsathe Jun 28 '11 at 17:55
6

The concise version:

String dateStr = "06/27/2007";
DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date startDate = (Date)formatter.parse(dateStr);  

Add a try/catch block for a ParseException to ensure the format is a valid date.

Declan Lynch
  • 3,345
  • 17
  • 36
readikus
  • 369
  • 1
  • 6
  • 17
-7
var startDate = "06/27/2007";
startDate = new Date(startDate);

console.log(startDate);
MikeM
  • 27,227
  • 4
  • 64
  • 80