63

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
devman
  • 1,529
  • 2
  • 11
  • 24
  • 2
    Where are you seeing "3/1/112 11:49AM"? The returned value is a Date, not a string, so you must be doing *something* to see it as a string result... – Jon Skeet Mar 30 '12 at 14:38
  • I see Mon Mar 26 11:49:00 IST 2012 as output. – Chandra Sekhar Mar 30 '12 at 14:40
  • try to set the parsing to lenient using dateFormat.setLenient(true), and then check the results of your parsing – manub Mar 30 '12 at 14:40
  • Maybe your avd is not properly configured. Sometimes it happens to me. – Marcos Mar 30 '12 at 14:45
  • by the way use jodatime to avoid corner cases like this http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result/6841479#6841479 – Win Myo Htet Mar 30 '12 at 15:30

6 Answers6

120

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
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • everyone was right. it was converting correctly but i was using the Date.getYear(), getMonth() and all those which i was using incorrectly. Thanks for all you help. – devman Mar 30 '12 at 14:56
19

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);
mDonmez
  • 400
  • 4
  • 14
3
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

skamlet
  • 693
  • 7
  • 23
fish40
  • 5,738
  • 17
  • 50
  • 69
2

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

Jose Kurian
  • 703
  • 7
  • 10
0
GregorianCalendar date;

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

Toast.makeText(LogmeanActivity.this,dateForMart,Toast.LENGTH_LONG).show();
Robert
  • 5,278
  • 43
  • 65
  • 115
0
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
maxx777
  • 1,320
  • 1
  • 20
  • 37