how to convert java string to Date object

JavaStringDate

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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();
}

Solution 2 - Java

"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

Solution 3 - Java

    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..

Solution 4 - Java

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.

Solution 5 - Java

var startDate = "06/27/2007";
startDate = new Date(startDate);

console.log(startDate);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser755043View Question on Stackoverflow
Solution 1 - Javacitizen connView Answer on Stackoverflow
Solution 2 - JavasotherView Answer on Stackoverflow
Solution 3 - JavamihsatheView Answer on Stackoverflow
Solution 4 - JavareadikusView Answer on Stackoverflow
Solution 5 - JavabeingbradView Answer on Stackoverflow