0

I am just plugging and playing with a sample code to get a hang of chatting/messaging apps. I am using a function gettimedate such that, in my model class I have the following:

private volatile long mTimedate;

public myclass(....,DateType date,...){
        mTimedate = date != null ? date.time : new Date().getTime();

}
public final long getTimedate() {
        return mTimedate;
    }
public int hashCode() {
        if (mId != null) {
            return mId.hashCode();
        }

        int hash = mBody == null ? 0 : mBody.hashCode();
        return 31 * hash + (new Long(mTimedate)).hashCode();
    }
public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof myclass)) {
            return false;
        }

        myclass other = (myclass) obj;
        return mId != null ? mId.equals(other.mId) : other.mId == null
                && (mBody == null ? other.mBody == null : mBody.equals(other.mBody))
                && mTimedate == other.mdate;
    }

Now, I wonder, how do I use this time date function, such that I can extract "Month -date, year " for top of my view and for the bottom part "time in 12 hr format" from the same function gettimedate().

Here's my adapter where I am using this function for display:

 holder.txtInfo.setText(String.valueOf(new Date(msg.getTimedate())));

which returns the whole package as say "Mon Oct 19 ,13:03:03 EDT 2015" but I only need to use it in pieces for different views. Any clue how I can go about it to use it in 2 different places?

P.S: For a rough working code sample , here's a sample code: http://www.codeproject.com/Tips/897826/Designing-Android-Chat-Bubble-Chat-UI

Also , i would like to modify this test code to see if I can show the date on top for the day of the conversation only once and the just the 12hr time format for each and every conversation ,example( 4:35pm) , just like in any standard chat. Is there anyway to do that with the above code?

Thanks!

3 Answers3

0

If you want to manipulate and working with details of date,I prefere you to use Joda Time.It's really very helpfull library,you can find Interval,Period,Duration and more and more...

Here is the link of .jar file of library.Good luck

nuridin selimko
  • 332
  • 1
  • 11
0

Use the SimpleDateFormat class.

Example:

public String getDateTimeStringWithSeconds(long time) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(time);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d/M-yyyy - HH:mm:ss");
    return simpleDateFormat.format(calendar.getTime());
}

And that will return a string such as "20/10-2015 - 00:58:33".
But the way its printed can be changed to almost anything, it's all in the SimpleDateFormat documentation.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • thanks I got that part working, however, i was wondering how do I display a single day on top of every chat with the format "tuesday, october 20 ,2015" for every chat received on that particular day without repetition,just like in the format of android native text message. –  Oct 19 '15 at 23:40
0

Use this:

Date date = new Date();
SimpleDateFormat tryDateFormat = new SimpleDateFormat("EEEE MMMM-dd-yyyy");
String formattedDate = tryDateFormat.format(date);

System.out.println(formattedDate);

which prints this

Tuesday October-20-2015
Jeongbebs
  • 4,100
  • 7
  • 34
  • 60
  • I got that part working but for the second part how do I get it to display on top of the listview for every new batch of messages ,just once, for the messages from that particular date ? like in android native messages –  Oct 20 '15 at 10:33