Convert string to Date in java

JavaAndroidDate

Java Problem Overview


I'm trying to parse a string to a date field in an android application but I can't seem to get it correct. Here is the string I'm trying to convert to a date "03/26/2012 11:49:00 AM". The function I'm using is:

private Date ConvertToDate(String dateString){
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return convertedDate;
}

But I keep getting 3/1/112 11:49AM as the result.

Java Solutions


Solution 1 - Java

You are wrong in the way you display the data I guess, because for me:

    String dateString = "03/26/2012 11:49:00 AM";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(convertedDate);

Prints:

Mon Mar 26 11:49:00 EEST 2012

Solution 2 - Java

it went OK when i used Locale.US parametre in SimpleDateFormat

String dateString = "15 May 2013 17:38:34 +0300";
System.out.println(dateString);

SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z", Locale.US);
DateFormat targetFormat = new SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault());
String formattedDate = null;
Date convertedDate = new Date();
try {
     convertedDate = dateFormat.parse(dateString);
System.out.println(dateString);
formattedDate = targetFormat.format(convertedDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 System.out.println(convertedDate);

Solution 3 - Java

String str_date="13-09-2011";
DateFormat formatter ; 
Date date ; 
formatter = new SimpleDateFormat("dd-MM-yyyy");
date = (Date)formatter.parse(str_date); 
System.out.println("Today is " +date.getTime());

Try this

Solution 4 - Java

This code will help you to make a result like FEB 17 20:49 .

    String myTimestamp="2014/02/17 20:49";

	SimpleDateFormat form = new SimpleDateFormat("yyyy/MM/dd HH:mm");
	Date date = null;
	Date time = null;
	try 
	{
	    date = form.parse(myTimestamp);
	    time = new Date(myTimestamp);
	    SimpleDateFormat postFormater = new SimpleDateFormat("MMM dd");
	    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
	    String newDateStr = postFormater.format(date).toUpperCase();
	    String newTimeStr = sdf.format(time);
	    System.out.println("Date  : "+newDateStr);
	    System.out.println("Time  : "+newTimeStr);
	}
	catch (Exception e) 
    {
	    e.printStackTrace();
	}

Result :

Date : FEB 17

Time : 20:49

Solution 5 - Java

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";

try {

	Date date = formatter.parse(dateInString);
	System.out.println(date);
	System.out.println(formatter.format(date));

} catch (ParseException e) {
	e.printStackTrace();
}

Output:

2014/08/06 16:06:54
2014/08/06 16:06:54

Solution 6 - Java

GregorianCalendar date;

CharSequence dateForMart = android.text.format.DateFormat.format("yyyy-MM-dd", date);

Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show();

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
QuestiondevmanView Question on Stackoverflow
Solution 1 - JavaBoris StrandjevView Answer on Stackoverflow
Solution 2 - JavamDonmezView Answer on Stackoverflow
Solution 3 - Javafish40View Answer on Stackoverflow
Solution 4 - JavaJose KurianView Answer on Stackoverflow
Solution 5 - JavaShivendra Prakash ShuklaView Answer on Stackoverflow
Solution 6 - JavaĐỗ Trọng AnView Answer on Stackoverflow