0

I am trying to convert a string to DateTime format. My string is 23 Feb 2014 14:00:48. I want to send to the server in 2011-04-08T12:29:00.000Z format.

I am doing as below

String str = departetforget.getText().toString();
        System.out.println("printing date"+str);
        String TIMEZONE_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";
        try {
            Date date = new SimpleDateFormat(TIMEZONE_DATE_TIME_FORMAT, Locale.US).parse(str);
            System.out.println("printing parsed date "+date);
        } catch (ParseException e) {
            System.out.println("printing date Exception ==> "+e.toString());
            e.printStackTrace();
        }

But I am getting parse exception.

user3322955
  • 348
  • 1
  • 15
teekib
  • 2,821
  • 10
  • 39
  • 75
  • looks like a repost of [this](http://stackoverflow.com/questions/2580925/simpledateformat-parsing-date-with-z-literal) – cosmincalistru Feb 19 '14 at 08:52
  • Have you check to respect the pattern format? [Java SimpleDateFormat](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) – phemt.latd Feb 19 '14 at 08:53

4 Answers4

9

Try this tested working demo

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        System.out.println("Converted Date : " + convertDate("23 Feb 2014 14:00:48"));
    }

    private String convertDate(String date) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy hh:mm:ss");
            Date d = format.parse(date);
            SimpleDateFormat serverFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            return serverFormat.format(d);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}

output:

Converted Date : 2014-02-23T14:00:48.000Z

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
2

You need 2 SimpleDateFormats to convert Date string. For Example:

    String str = departetforget.getText().toString();
    String MY_FORMAT = "dd MMM yyyy HH:mm:ss";
    String TIMEZONE_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";
    try {
        Date date = new SimpleDateFormat(MY_FORMAT, Locale.US).parse(str);

        String timezoneString = new SimpleDateFormat(MY_FORMAT, Locale.US).format(date);
    } catch (ParseException e) {
        System.out.println("printing date Exception ==> "+e.toString());
        e.printStackTrace();

    }
Roman Black
  • 3,501
  • 1
  • 22
  • 31
  • Hi when converting second time i.e; String timezoneString ,i think we need to replace it by TIMEZONE_DATE_TIME_FORMAT ..i did it is giving out but as 2014-02-19T14:26:58GMT+05:30, but i dont need GMT, i want it as 2011-04-08T12:29:00.000Z – teekib Feb 19 '14 at 09:04
  • Yes, I'm sorry. You need to replace it by TIMEZONE_DATE_FORMAT. – Roman Black Feb 19 '14 at 09:30
  • If you don't want GMT use 'Z' instead 'z' – Roman Black Feb 19 '14 at 09:34
  • 1
    For date that You provide You need to use this pattern yyyy-MM-dd'T'HH:mm:ss.SSS'Z' – Roman Black Feb 19 '14 at 09:37
0
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            String newFormat = formatter.format(testDate);

String date = "2011-11-12 16:05:06";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd HH:MM:SS");
        Date testDate = null;
        try {
            testDate = sdf.parse(date);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String newFormat = formatter.format(testDate);
        System.out.println(".....Date..."+newFormat);
Exceptional
  • 2,994
  • 1
  • 18
  • 25
0

You have to Parse the given date with right format.

String TIMEZONE_DATE_TIME_PARSE="dd MMM yyyy HH:mm:ss";

And then format it with SimpleDateFormat like sdf.format(date)

String TIMEZONE_DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";
sandy
  • 3,311
  • 4
  • 36
  • 47