Java Reference
In-Depth Information
Code 8.1
continued
Source code of the
MessagePost class
/**
* Display the details of this post.
*
* (Currently: Print to the text terminal. This is simulating
* display in a web browser for now.)
*/
public void display()
{
System.out.println(username);
System.out.println(message);
System.out.print(timeString(timestamp));
if (likes > 0) {
System.out.println( " — " + likes + " people like this." );
}
else {
System.out.println();
}
if (comments.isEmpty()) {
System.out.println( " No comments." );
}
else {
System.out.println( " " + comments.size() +
" comment(s). Click here to view." );
}
}
/**
* Create a string describing a time point in the past in relative
* terms, such as "30 seconds ago" or "7 minutes ago".
* Currently, only seconds and minutes are used for the string.
*
* @param time The time value to convert (in system milliseconds)
* @return A relative time string for the given time
*/
private String timeString( long time)
{
long current = System.currentTimeMillis();
long pastMillis = current — time; // time passed in millisecs
long seconds = pastMillis / 1000;
long minutes = seconds / 60;
if (minutes > 0) {
return minutes + " minutes ago" ;
}
Search WWH ::




Custom Search