Rey_mysterio
Rey_mysterio

Reputation: 81

Android:Message created time is not displayed in timestamp

I have to display the message created time in Timestamp for chat application now my all the timestamp it automatically shows the device time When I open the app its not showing the message created time.check my code and change me If i did wrong.

public static String getFormatedTime(long dateTime,String timeZone, String format){
    String time = null;
    try{
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(dateTime);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
        time = sdf.format(cal.getTime());
    }
    catch(Exception e){
        //logger.log(Level.SEVERE,"\n ERROR**********, Exception during get formated time: "+e+"\n");       
    }
    return time;
}

Calendar calendar = Calendar.getInstance();
TimeZone zone = calendar.getTimeZone();
String timeZone = zone.getID();

String msgAtTime =  getFormattedTime(System.currentTimeInMillis(), timezone, "MMM dd, hh:mm a");

msgTextVies.setText(msgAtTime);

I want to show the message created time like this using above code:

// show dateTime

DateTime createdAt = message.getCreatedAt();

But I dont know how to implement it by using previous code.

Upvotes: 2

Views: 512

Answers (2)

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23606

You can do it by two ways.

1. By Device side database: Make database in Device which store the message information and its time when you get it. And populate that database data in to your app. So you will get all the posted message time in your timeline.

2. By Server Side database: Hope you are calling web service to send message from one user to another. At that time, also pass the current time of your device. Or either make system that when ever it gets any entry there will be also entry for current time stamp for that table. So with this, whenever you call web service, you surly get message posted time. Populate that time in the List.

Please let me know if you are not getting me clear.

Upvotes: 1

Chintan Khetiya
Chintan Khetiya

Reputation: 16152

Simply you can use below code to get Message Created time.

public String getCreatedAt()
{
    Calendar c1 = Calendar.getInstance();      
    String msg_time= c1.get(Calendar.YEAR) + c1.get(Calendar.MONTH) + c1.get(Calendar.DAY_OF_MONTH) + c1.get(Calendar.HOUR_OF_DAY) + c1.get(Calendar.MINUTE);
    return msg_time;
}

I have not set any specific format , but you can adjust as per your requirement.

Hope it works for you.

Upvotes: 2

Related Questions