How can I convert this date to Java?

I want to convert:

2010-03-15T16:34:46Z

into something like 5 hours ago

How to do it in Java?

+2
source share
4 answers

JodaTime supports custom format parsing. See DateTimeFormatterBuilder and DateTimeBuilder.parseDateTime ().

Once you have a DateTime, you can create a Duration or Period from this and the current time and use a different formatter for beautiful printing. See the Example PeriodFormatter referenced by BalusC in the comments above.]

+1
source

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

Java , :) ( Java)

0
     Calendar calendar = new GregorianCalendar(2010,Calendar.March,15, 16,34,46);
     calendar.add(Calendar.HOUR,-5);
0

TL;DR

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

5

java.time

java.time, .

Instant

. , ISO 8601. java.time / . .

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

.

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

.

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

Duration

-- Duration.

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

Java 9 to…Part, , , , , . Java 8, Java 9 .

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

5

ISO 8601

, ISO 8601, , Duration::toString: PnYnMnDTnHnMnS

P . T -- --.

, :

PT5H

Duration .

Duration d = Duration.parse( "PT5H" ) ;
0

Source: https://habr.com/ru/post/1766577/


All Articles