How to Calculate "Time Ago" in Java

How to calculate time ago in Java?

Take a look at the PrettyTime library.

It's quite simple to use:

import org.ocpsoft.prettytime.PrettyTime;

PrettyTime p = new PrettyTime();
System.out.println(p.format(new Date()));
// prints "moments ago"

You can also pass in a locale for internationalized messages:

PrettyTime p = new PrettyTime(new Locale("fr"));
System.out.println(p.format(new Date()));
// prints "à l'instant"

As noted in the comments, Android has this functionality built into the android.text.format.DateUtils class.

How to get Time ago in java

Add below maven dependency

<dependency>
<groupId>org.ocpsoft.prettytime</groupId>
<artifactId>prettytime</artifactId>
<version>1.0.8.Final</version>
</dependency>

Here is sample code for

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import org.ocpsoft.pretty.time.PrettyTime;

public class TestClass {

public static void main(String[] args) {

LocalDateTime currentTime = LocalDateTime.now();
Date before5HrDate = Date.from(currentTime.minusHours(5).atZone(ZoneId.systemDefault()).toInstant());

PrettyTime p = new PrettyTime();
String is = p.format(before5HrDate);

System.out.println("Is : " + is );
}

}

Answer : Is : 5 hours ago

How to calculate Time ago in in java(jsp)

For this case it is best to use LocalDateTime.

First you have to convert the two strings into a LocalDateTime

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

LocalDateTime currentTime = LocalDateTime.parse(currentTimeString, formatter); // Or use LocalDateTime.now()
LocalDateTime postTime = LocalDateTime.parse(postTimeString, formatter);

These objects can then be used to determine the time between them. There are two possibilities to do this. All of them give the same result, which you can use to determine the time between them according to your own preferences.

Possibility 1:

long seconds = postTime.until(currentTime, ChronoUnit.SECONDS);

Possibility 1:

long seconds = ChronoUnit.SECONDS.between(postTime, currentTime);

You can also get more information here:

  1. Introduction to the Java 8 Date/Time API
  2. Java 8: Difference between two LocalDateTime in multiple units

How to convert Timestamp to Date and calculate to time ago?

I think this answered question solves your's also.

Android difference between Two Dates

Cast your String from the server to a Date object, then substract current day - what you got from the server.

Get current date with :

import java.util.Calendar

Date currentTime = Calendar.getInstance().getTime();

Time ago for Android/java

What you want to display is called as the Relative time display. Android provides methods
to display time relative to your current time. You don't have to use any third party library just for this purpose.

You can use

DateUtils.getRelativeTimeSpanString(long time, long now, long minResolution)

Refer docs Here

eg.

DateUtils.getRelativeTimeSpanString(your_time_in_milliseconds, current_ time_in_millisecinds,DateUtils.MINUTE_IN_MILLIS);

UPDATE1:

You can try following to pass your date and get the milliseconds for it.

public static long getDateInMillis(String srcDate) {
SimpleDateFormat desiredFormat = new SimpleDateFormat(
"d MMMM yyyy, hh:mm aa");

long dateInMillis = 0;
try {
Date date = desiredFormat.parse(srcDate);
dateInMillis = date.getTime();
return dateInMillis;
} catch (ParseException e) {
Log.d("Exception while parsing date. " + e.getMessage());
e.printStackTrace();
}

return 0;
}

Hope it helps you.

How can I calculate a time difference in Java?

String time1 = "16:00:00";
String time2 = "19:00:00";

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1 = format.parse(time1);
Date date2 = format.parse(time2);
long difference = date2.getTime() - date1.getTime();

Difference is in milliseconds.

I modified sfaizs post.

How to calculate relative time spans using Java (code converting problem)?

You need to gain a deeper understanding of what this method actually does. Literally translating code from C# to Java won't give you a good solution and gets you stuck on language-specific details.

The two lines basically calculate the (absolute) difference in seconds of a timestamp to the current time. This can be written in Java as follows:

Duration duration = Duration.between(LocalDateTime.now(), timestamp);
long delta = duration.abs().getSeconds();

I'm just addressing your actual question here on how to transform these two lines. The provided snippet is not valid Java code and some parts are missing. delta is the difference in seconds which does not necessarily need to be a double. The argument you pass to your method should be named anything else than now because this is the timestamp you want to compare to the current time inside the method.

Calculate date/time difference in java

try

long diffSeconds = diff / 1000 % 60;  
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);

NOTE: this assumes that diff is non-negative.



Related Topics



Leave a reply



Submit