2

I want to convert:

2010-03-15T16:34:46Z

into something like "5 hours ago"

How can I do this in Java?

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • 2
    Are you open to using [JodaTime](http://joda-time.sourceforge.net/)? See [this answer](http://stackoverflow.com/questions/2179644/how-to-calculate-elapsed-time-from-now-with-joda-time/2179831#2179831) for an example. Doing so using standard `java.util.Calendar` API would only lead to verbose code and headaches. – BalusC Sep 25 '10 at 01:08

4 Answers4

1

JodaTime supports parsing from a user-defined format. See DateTimeFormatterBuilder and DateTimeBuilder.parseDateTime().

Once you have a DateTime, you can create a Duration or Period from that and the current time, and use another formatter to pretty-print. [See the PeriodFormatter example referenced by BalusC in comments above.]

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • 1
    The Joda-Time project is now in maintenance mode, with the team advising migration to the *java.time* classes built into Java 8 and later. – Basil Bourque Apr 05 '18 at 15:15
0

I know a plugin in Jquery for this : http://plugins.jquery.com/project/CuteTime

For Java i assume you will need to use your brain :) ( You can translate it to Java )

malletjo
  • 1,766
  • 16
  • 18
0
     Calendar calendar = new GregorianCalendar(2010,Calendar.March,15, 16,34,46);
     calendar.add(Calendar.HOUR,-5);
Chuckie
  • 125
  • 2
0

tl;dr

Duration.between(
    Instant.parse( "2010-03-15T16:34:46Z" ) , 
    Instant.now() 
)     
.toHours()    // returns a `long` integer number. 
+ " hours ago"

5 hours ago

java.time

The modern approach uses the java.time classes that supplant the troublesome old legacy date-time classes.

Instant

Parse your input string. That string uses a format defined in the ISO 8601 standard. The java.time classes use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

Instant instant = Instant.parse( "2010-03-15T16:34:46Z" ) ;

Get our later moment.

Instant later = instant.now() ;  // Capture the current moment in UTC.

Let’s use an moment five hour later.

Instant later = instant.plus( 5L , ChronoUnit.HOURS ) ;

Duration

Represent elapsed time of hours-minutes-seconds with Duration class.

Duration d = Duration.between( instant , later ) ;

If you want to report the entire elapsed time as one big number of hours, call Duration::toHours.

String output = d.toHoursPart() + " hours ago" ;

5 hours ago

In Java 9 and later, call to…Part to get each part of days, hours, minutes, seconds, nanoseconds. These methods were strangely missing in Java 8, but added in Java 9 and later.

long daysPart = d.toDaysPart() ;
int hoursPart = d.toHoursPart() ;
int minutesPart = d.toMinutesPart() ;
String output = … ;  // Use the parts to generate your output.

ISO 8601 duration

You may find the ISO 8601 compliant string for durations generated by Duration::toString to be useful: PnYnMnDTnHnMnS

The P marks the beginning. The T separates any years-months-days from any hours-minutes-seconds.

So our example above for five hours would be:

PT5H

Such strings can be parsed into Duration hours.

Duration d = Duration.parse( "PT5H" ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Nice, thank you! Should toHoursPart just be toHours? – Lou Morda Jul 24 '20 at 03:15
  • @LouMorda If you want the entire duration as one big number of hours, call `Duration::toHours`. More likely, you will want the number of 24-hour generic days, and the hours, and the minutes, and perhaps seconds. If so, in that case do as I mentioned in the Answer, call `to…Part` methods for each of those parts. I will revise the text to make this more clear. Thanks for asking. – Basil Bourque Jul 24 '20 at 04:35