-3

I want to get date from string and again string from date. I have tried using simple date format but textview shows no value.

   df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
   Date date = new Date();
            Date date1 = new Date();

            startTime = mEvent.getFromDate();
            date = df.parse(startTime);
            df = new SimpleDateFormat("HH:mm a");
            startTime = df.format(date);
            showFromTime.setText(startTime);

I want to get date string from class mEvent is an instance of class , and I want to convert string into hours and mins format. start time is string and showFromTime is a text view. It dose not show any time.

Whats going wrong?

Sid
  • 2,792
  • 9
  • 55
  • 111

1 Answers1

1

You have to first parse the String into Date object first using SimpleDateFormat. Then only you format the Date object again using a new SimpleDateFormat. I have done some modification of your code. You may try and see if it is working. You have to be really sure the date format you get from mEvent object matches "MM/dd/yyyy hh:mm:ss a" format.

    SimpleDateFormat df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
    SimpleDateFormat df2 = new SimpleDateFormat("HH:mm a");

    Date convertedDate = new Date();
    try {
        convertedDate = df.parse(mEvent.getFromDate());
    } catch (ParseException ex) {
        //do nothing
    }
    startTime = df2.format(convertedDate);
    showFromTime.setText(startTime);
kopikaokao
  • 500
  • 7
  • 13
  • got the time but getting current time. I want date which i have saved. – Sid Jan 31 '16 at 10:46
  • @Sid Try to write a log in catch clause because i believe ParseException is catch because you provided the wrong format for SimpleDateFormat to parse the string. Can i know what will mEvent.getFromDate() return ? – kopikaokao Jan 31 '16 at 10:48
  • Sun Jan 31 01:00:35 GMT+05:30 2016 this format date i get. – Sid Jan 31 '16 at 10:49
  • I am saving this with getTime() function. I have not provided format to save datte. – Sid Jan 31 '16 at 10:50
  • @Sid try change SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa"); to SimpleDateFormat df = new SimpleDateFormat("EE MM dd HH:mm:ss z yyyy"); – kopikaokao Jan 31 '16 at 10:55
  • converted date i am getting current date Sun Jan 31 16:25:22 GMT+05:30 2016. not from mEvent.getFromDate() – Sid Jan 31 '16 at 10:57
  • Hold on a sec. Sun Jan 31 01:00:35 GMT+05:30 2016 << Is this what you get when you call mEvent.getFromDate() ? I want to know exactly what getFromDate() return. – kopikaokao Jan 31 '16 at 10:58
  • yes this is I am getting in return – Sid Jan 31 '16 at 11:00
  • I tried toput this String dateFrom = mEvent.getFromDate(); convertedDate = df.parse(dateFrom); when i debug at dateFrom shows Sun Jan 31 00:00:34 GMT+05:30 2016 – Sid Jan 31 '16 at 11:09
  • but converted date is :Sun Jan 31 16:39:16 GMT+05:30 2016.. why is it like this? – Sid Jan 31 '16 at 11:10
  • I've edited the answer please check. This is the correct format to parse the date string. SimpleDateFormat df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy"); – kopikaokao Jan 31 '16 at 11:18
  • Got it. Thank you so much.. @Styx – Sid Jan 31 '16 at 11:25
  • @Sid welcome. You can lookup this documentation to learn more about SimpleDateFormat [link](http://developer.android.com/intl/es/reference/java/text/SimpleDateFormat.html) – kopikaokao Jan 31 '16 at 11:26