GeezerVison is showing github.com — adding code for calculating time ago in Java for both version 7 and v… · FEDENG01/tutorials@e31a4df
File treeExpand file treeCollapse file tree Open diff view settings core-java-modules/core-java-date-operations-2 main/java/com/baeldung/timeago test/java/com/baeldung/timeago
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: core-java-modules/core-java-date-operations-2/pom.xml +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 30 | 30 | <artifactId>hirondelle-date4j</artifactId> | 31 | 31 | <version>${hirondelle-date4j.version}</version> | 32 | 32 | </dependency> | | 33 | + <dependency> | | 34 | + <groupId>net.time4j</groupId> | | 35 | + <artifactId>time4j-base</artifactId> | | 36 | + <version>${time4j-base.version}</version> | | 37 | + </dependency> | | 38 | + <dependency> | | 39 | + <groupId>net.time4j</groupId> | | 40 | + <artifactId>time4j-sqlxml</artifactId> | | 41 | + <version>${time4j-sqlxml.version}</version> | | 42 | + </dependency> | | 43 | + <dependency> | | 44 | + <groupId>org.ocpsoft.prettytime</groupId> | | 45 | + <artifactId>prettytime</artifactId> | | 46 | + <version>${prettytime.version}</version> | | 47 | + </dependency> | 33 | 48 | </dependencies> | 34 | 49 |
| 35 | 50 | <properties> | 36 | 51 | <joda-time.version>2.10</joda-time.version> | 37 | 52 | <hirondelle-date4j.version>1.5.1</hirondelle-date4j.version> | | 53 | + <prettytime.version>3.2.7.Final</prettytime.version> | | 54 | + <time4j-base.version>5.9</time4j-base.version> | | 55 | + <time4j-sqlxml.version>5.8</time4j-sqlxml.version> | 38 | 56 | </properties> | 39 | 57 |
| 40 | 58 | </project> |
Collapse file Copy file name to clipboard +94Lines changed: 94 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.timeago.version7; | | 2 | + | | 3 | +import java.util.Date; | | 4 | +import java.util.TimeZone; | | 5 | + | | 6 | +import org.joda.time.DateTime; | | 7 | +import org.joda.time.DateTimeZone; | | 8 | +import org.joda.time.Period; | | 9 | +import org.joda.time.format.PeriodFormat; | | 10 | +import org.joda.time.format.PeriodFormatter; | | 11 | +import org.joda.time.format.PeriodFormatterBuilder; | | 12 | + | | 13 | +public class TimeAgoCalculator { | | 14 | + | | 15 | + public static String calculateTimeAgoByTimeGranularity(Date pastTime, TimeGranularity granularity) { | | 16 | + Date currentTime = new Date(); | | 17 | + long timeDifferenceInMillis = currentTime.getTime() - pastTime.getTime(); | | 18 | + return timeDifferenceInMillis / granularity.toMillis() + " " + granularity.name() | | 19 | + .toLowerCase() + " ago"; | | 20 | + } | | 21 | + | | 22 | + public static String calculateHumanFriendlyTimeAgo(Date pastTime) { | | 23 | + Date currentTime = new Date(); | | 24 | + long timeDifferenceInMillis = currentTime.getTime() - pastTime.getTime(); | | 25 | + if (timeDifferenceInMillis / TimeGranularity.DECADES.toMillis() > 0) | | 26 | + return "several decades ago"; | | 27 | + else if (timeDifferenceInMillis / TimeGranularity.YEARS.toMillis() > 0) | | 28 | + return "several years ago"; | | 29 | + else if (timeDifferenceInMillis / TimeGranularity.MONTHS.toMillis() > 0) | | 30 | + return "several months ago"; | | 31 | + else if (timeDifferenceInMillis / TimeGranularity.WEEKS.toMillis() > 0) | | 32 | + return "several weeks ago"; | | 33 | + else if (timeDifferenceInMillis / TimeGranularity.DAYS.toMillis() > 0) | | 34 | + return "several days ago"; | | 35 | + else if (timeDifferenceInMillis / TimeGranularity.HOURS.toMillis() > 0) | | 36 | + return "several hours ago"; | | 37 | + else if (timeDifferenceInMillis / TimeGranularity.MINUTES.toMillis() > 0) | | 38 | + return "several minutes ago"; | | 39 | + else | | 40 | + return "moments ago"; | | 41 | + } | | 42 | + | | 43 | + public static String calculateExactTimeAgoWithJodaTime(Date pastTime) { | | 44 | + Period period = new Period(new DateTime(pastTime.getTime()), new DateTime()); | | 45 | + PeriodFormatter formatter = new PeriodFormatterBuilder().appendYears() | | 46 | + .appendSuffix(" year ", " years ") | | 47 | + .appendSeparator("and ") | | 48 | + .appendMonths() | | 49 | + .appendSuffix(" month ", " months ") | | 50 | + .appendSeparator("and ") | | 51 | + .appendWeeks() | | 52 | + .appendSuffix(" week ", " weeks ") | | 53 | + .appendSeparator("and ") | | 54 | + .appendDays() | | 55 | + .appendSuffix(" day ", " days ") | | 56 | + .appendSeparator("and ") | | 57 | + .appendHours() | | 58 | + .appendSuffix(" hour ", " hours ") | | 59 | + .appendSeparator("and ") | | 60 | + .appendMinutes() | | 61 | + .appendSuffix(" minute ", " minutes ") | | 62 | + .appendSeparator("and ") | | 63 | + .appendSeconds() | | 64 | + .appendSuffix(" second", " seconds") | | 65 | + .toFormatter(); | | 66 | + return formatter.print(period); | | 67 | + } | | 68 | + | | 69 | + public static String calculateHumanFriendlyTimeAgoWithJodaTime(Date pastTime) { | | 70 | + Period period = new Period(new DateTime(pastTime.getTime()), new DateTime()); | | 71 | + if (period.getYears() != 0) | | 72 | + return "several years ago"; | | 73 | + else if (period.getMonths() != 0) | | 74 | + return "several months ago"; | | 75 | + else if (period.getWeeks() != 0) | | 76 | + return "several weeks ago"; | | 77 | + else if (period.getDays() != 0) | | 78 | + return "several days ago"; | | 79 | + else if (period.getHours() != 0) | | 80 | + return "several hours ago"; | | 81 | + else if (period.getMinutes() != 0) | | 82 | + return "several minutes ago"; | | 83 | + else | | 84 | + return "moments ago"; | | 85 | + } | | 86 | + | | 87 | + public static String calculateZonedTimeAgoWithJodaTime(Date pastTime, TimeZone zone) { | | 88 | + DateTimeZone dateTimeZone = DateTimeZone.forID(zone.getID()); | | 89 | + Period period = new Period(new DateTime(pastTime.getTime(), dateTimeZone), new DateTime(dateTimeZone)); | | 90 | + return PeriodFormat.getDefault() | | 91 | + .print(period); | | 92 | + } | | 93 | + | | 94 | +} |
Collapse file Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.timeago.version7; | | 2 | + | | 3 | +import java.util.concurrent.TimeUnit; | | 4 | + | | 5 | +public enum TimeGranularity { | | 6 | + SECONDS { | | 7 | + public long toMillis() { | | 8 | + return TimeUnit.SECONDS.toMillis(1); | | 9 | + } | | 10 | + }, MINUTES { | | 11 | + public long toMillis() { | | 12 | + return TimeUnit.MINUTES.toMillis(1); | | 13 | + } | | 14 | + }, HOURS { | | 15 | + public long toMillis() { | | 16 | + return TimeUnit.HOURS.toMillis(1); | | 17 | + } | | 18 | + }, DAYS { | | 19 | + public long toMillis() { | | 20 | + return TimeUnit.DAYS.toMillis(1); | | 21 | + } | | 22 | + }, WEEKS { | | 23 | + public long toMillis() { | | 24 | + return TimeUnit.DAYS.toMillis(7); | | 25 | + } | | 26 | + }, MONTHS { | | 27 | + public long toMillis() { | | 28 | + return TimeUnit.DAYS.toMillis(30); | | 29 | + } | | 30 | + }, YEARS { | | 31 | + public long toMillis() { | | 32 | + return TimeUnit.DAYS.toMillis(365); | | 33 | + } | | 34 | + }, DECADES { | | 35 | + public long toMillis() { | | 36 | + return TimeUnit.DAYS.toMillis(365 * 10); | | 37 | + } | | 38 | + }; | | 39 | + | | 40 | + public abstract long toMillis(); | | 41 | +} |
Collapse file Copy file name to clipboard +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.timeago.version8; | | 2 | + | | 3 | +import java.time.Duration; | | 4 | +import java.time.LocalDate; | | 5 | +import java.time.LocalDateTime; | | 6 | +import java.time.Period; | | 7 | +import java.time.ZoneId; | | 8 | +import java.util.Date; | | 9 | +import java.util.Locale; | | 10 | + | | 11 | +import org.ocpsoft.prettytime.PrettyTime; | | 12 | + | | 13 | +public class TimeAgoCalculator { | | 14 | + | | 15 | + public static String calculateTimeAgoWithPeriodAndDuration(LocalDateTime pastTime, ZoneId zone) { | | 16 | + Period period = Period.between(pastTime.toLocalDate(), LocalDate.now(zone)); | | 17 | + Duration duration = Duration.between(pastTime, LocalDateTime.now(zone)); | | 18 | + if (period.getYears() != 0) | | 19 | + return "several years ago"; | | 20 | + else if (period.getMonths() != 0) | | 21 | + return "several months ago"; | | 22 | + else if (period.getDays() != 0) | | 23 | + return "several days ago"; | | 24 | + else if (duration.toHours() != 0) | | 25 | + return "several hours ago"; | | 26 | + else if (duration.toMinutes() != 0) | | 27 | + return "several minutes ago"; | | 28 | + else if (duration.getSeconds() != 0) | | 29 | + return "several seconds ago"; | | 30 | + else | | 31 | + return "moments ago"; | | 32 | + } | | 33 | + | | 34 | + public static String calculateTimeAgoWithPrettyTime(Date pastTime) { | | 35 | + PrettyTime prettyTime = new PrettyTime(); | | 36 | + return prettyTime.format(pastTime); | | 37 | + } | | 38 | + | | 39 | + public static String calculateTimeAgoWithTime4J(Date pastTime, ZoneId zone, Locale locale) { | | 40 | + return net.time4j.PrettyTime.of(locale) | | 41 | + .printRelative(pastTime.toInstant(), zone); | | 42 | + } | | 43 | + | | 44 | +} |
Collapse file Copy file name to clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.timeago.version7; | | 2 | + | | 3 | +import java.util.Date; | | 4 | + | | 5 | +import org.junit.Assert; | | 6 | +import org.junit.Test; | | 7 | + | | 8 | +public class TimeAgoCalculatorUnitTest { | | 9 | + | | 10 | + @Test | | 11 | + public void timeAgoByTimeGranularityTest() { | | 12 | + long DAY_IN_MILLIS = 1000 * 60 * 60 * 24; | | 13 | + Assert.assertEquals("5 seconds ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 1000)), TimeGranularity.SECONDS)); | | 14 | + Assert.assertEquals("5 minutes ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 60 * 1000)), TimeGranularity.MINUTES)); | | 15 | + Assert.assertEquals("5 hours ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000)), TimeGranularity.HOURS)); | | 16 | + Assert.assertEquals("5 days ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS)), TimeGranularity.DAYS)); | | 17 | + Assert.assertEquals("5 months ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30)), TimeGranularity.MONTHS)); | | 18 | + Assert.assertEquals("5 weeks ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 7)), TimeGranularity.WEEKS)); | | 19 | + Assert.assertEquals("5 years ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365)), TimeGranularity.YEARS)); | | 20 | + Assert.assertEquals("5 decades ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365 * 10)), TimeGranularity.DECADES)); | | 21 | + } | | 22 | + | | 23 | + @Test | | 24 | + public void humanFriendlyTimeAgoTest() { | | 25 | + long DAY_IN_MILLIS = 1000 * 60 * 60 * 24; | | 26 | + Assert.assertEquals("moments ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 1000)))); | | 27 | + Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 60 * 1000)))); | | 28 | + Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000)))); | | 29 | + Assert.assertEquals("several days ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS)))); | | 30 | + Assert.assertEquals("several months ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30)))); | | 31 | + Assert.assertEquals("several weeks ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (3 * DAY_IN_MILLIS * 7)))); | | 32 | + Assert.assertEquals("several years ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365)))); | | 33 | + Assert.assertEquals("several decades ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365 * 10)))); | | 34 | + } | | 35 | + | | 36 | + @Test | | 37 | + public void calculateExactTimeAgoWithJodaTimeTest() { | | 38 | + Assert.assertEquals("5 hours and 15 minutes and 3 seconds", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000 + 15 * 60 * 1000 + 3 * 1000)))); | | 39 | + Assert.assertEquals("5 hours and 1 minute and 1 second", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1 * 1000)))); | | 40 | + Assert.assertEquals("2 days and 1 minute and 1 second", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (2 * 24 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1 * 1000)))); | | 41 | + } | | 42 | + | | 43 | + @Test | | 44 | + public void calculateHumanFriendlyTimeAgoWithJodaTimeTest() { | | 45 | + long DAY_IN_MILLIS = 1000 * 60 * 60 * 24; | | 46 | + Assert.assertEquals("moments ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 1000)))); | | 47 | + Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 1000)))); | | 48 | + Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000)))); | | 49 | + Assert.assertEquals("several days ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS)))); | | 50 | + Assert.assertEquals("several months ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30)))); | | 51 | + Assert.assertEquals("several weeks ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (3 * DAY_IN_MILLIS * 7)))); | | 52 | + Assert.assertEquals("several years ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365)))); | | 53 | + } | | 54 | + | | 55 | +} |
Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.baeldung.timeago.version8; | | 2 | + | | 3 | +import java.time.Instant; | | 4 | +import java.time.LocalDateTime; | | 5 | +import java.time.ZoneId; | | 6 | + | | 7 | +import org.junit.Assert; | | 8 | +import org.junit.Test; | | 9 | + | | 10 | +public class TimeAgoCalculatorUnitTest { | | 11 | + | | 12 | + @Test | | 13 | + public void calculateTimeAgoWithPeriodAndDurationTest() { | | 14 | + long DAY_IN_MILLIS = 1000 * 60 * 60 * 24; | | 15 | + Assert.assertEquals("moments ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault()), ZoneId.systemDefault())); | | 16 | + Assert.assertEquals("several seconds ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault())); | | 17 | + Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 60 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault())); | | 18 | + Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 60 * 60 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault())); | | 19 | + Assert.assertEquals("several days ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS)), ZoneId.systemDefault()), ZoneId.systemDefault())); | | 20 | + Assert.assertEquals("several months ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30)), ZoneId.systemDefault()), ZoneId.systemDefault())); | | 21 | + Assert.assertEquals("several years ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365)), ZoneId.systemDefault()), ZoneId.systemDefault())); | | 22 | + } | | 23 | +} |
|