-2

I want to convert timestamp to date in android, I tried this code but not get expected result.

My Code Is.

String timeStampST = "2016-12-13 09:47:11";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date convertedDate = new Date();
try {
    convertedDate = dateFormat.parse(timeStampST);
    } catch (ParseException e) {
    e.printStackTrace();
}

System.out.println(convertedDate);

Out Put

Tue Dec 13 16:15:02 GMT+05:30 2016

Expected Out Put

Tue Dec 13 2016

I need day in my output

byteC0de
  • 5,153
  • 5
  • 33
  • 66

2 Answers2

3

Kindly copy paste this answer. It will help you.

        String timeStampST = "2016-12-13 09:47:11";
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        String resultDate = null;
        try {
            date = fmt.parse(timeStampST);
            SimpleDateFormat fmtOut = new SimpleDateFormat("EEE MMM dd yyyy");
            resultDate = fmtOut.format(date);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

OUTPUT

resultDate = 12/13/2016

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
1

Use this method with string and input and output format.

  public static String parseDateToddMMyyyy(String time, String inputPattern, String outputPattern) {
    SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
    Date date;
    String str = null;
    try {
        date = inputFormat.parse(time);
        str = outputFormat.format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}
Newbie Android
  • 389
  • 1
  • 8