0

How to transform date to CharSequence . I used to convert date to string. But I get an Exception.

CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
            Long.parseLong(newsData.getNewsDateCreated()),
            System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);

How do I convert it correctly? My remote server return a date format: 2016-9-26 - I don't want this to appear. From date I need to convert it to time maybe?

The output should give me like this:

date

RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56

6 Answers6

3

If your format shows "00:00:00T2016-08-24" split format and take whichever you want. You have a choice.

In my case i want to show only date i did this ,but my format are like this "2016-08-24T00:00:00"

holder.tYorDate.setText(list.get(position).getDate().split("T")[0]);

I hope this helps.

Akhil
  • 61
  • 6
1
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

Date convertedDate = new Date();
Date currentDate = new Date();
try{
convertedDate = dateFormat.parse(newsData.getNewsDateCreated());
}
            catch (ParseException e)
            {
                e.printStackTrace();
            }

    public long getDifference(Date startDate, Date endDate){

        //milliseconds
        long different = endDate.getTime() - startDate.getTime();

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        return elapsedHours = different / hoursInMilli;

    }

This should do the job. Use getDifference(convertedDate, currentDate) wherever needed and use hours/ hour accordingly.

Karishnu Poddar
  • 313
  • 2
  • 11
0

use this

String uDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").format(new Date());

here new Date() is the current date

sajan
  • 389
  • 5
  • 11
  • This is the output format of my remote server, I need to convert it like this - http://stackoverflow.com/questions/19419374/android-convert-date-and-time-to-milliseconds – RoCkDevstack Sep 26 '16 at 12:23
0
String resultDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").format(new Date());
Mohit Trivedi
  • 699
  • 3
  • 13
0

First,we need to convert specific format into date object

For e.g

String yourdate = "Tue Apr 23 16:08:28 GMT+05:30 2013";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date mDate = dateFormat.parse(yourdate);

Here I prepared function which will help you to convert your date into chars

 public static String getTimeDifference(long mActual){

    long mCurrent=0;
    int diffInDays=0;

        mCurrent = System.currentTimeMillis();

        if (mCurrent == 0 || mActual == 0)
            return "";

        long diff = mCurrent - mActual;

        diffInDays = (int) (diff / (1000 * 60 * 60 * 24));

    if (diffInDays > 0) {

            return diffInDays + "days ago";

        } else {

            int diffHours = (int) (diff / (60 * 60 * 1000));
            if (diffHours > 0) {
                return diffHours + "hours ago";
            } else {

                int diffMinutes = (int) ((diff / (60 * 1000) % 60));
                if(diffMinutes == 0)
                    return "Just Now";
                return diffMinutes + "minutes ago";
            }
        }

}

So Here you go:

getTimeDifference(mDate.getTime);
Jai
  • 1,974
  • 2
  • 22
  • 42
0

you can use below code

String str_date = "2014-11-26";

SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-mm-dd");
SimpleDateFormat writeFormat = new SimpleDateFormat("MMM dd,yyyy");

java.util.Date date;

date = readFormat.parse(str_date);

and for your desired date format you may refer this link too SimpleDateFormat

Arpan24x7
  • 648
  • 5
  • 24