How do I display date and time separately from a give datetime function?

℡╲_俬逩灬. 提交于 2019-12-13 07:17:03

问题


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!


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/33224880/how-do-i-display-date-and-time-separately-from-a-give-datetime-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!