-1

I have a SQL Timestamp String, e. g.

String timestamp = 2016-12-11 14:26:35

I want to get the difference to the day today, and the String in the form

11.12.206 14:26:35

I think to do this, I have to get the milliseconds from the object and parse them to a date / calendar object?

How to do this or is there an easier way?

writzlpfrimpft
  • 333
  • 3
  • 14
  • Are you working with Java 8? The best answers may depend on this. With Java 1.7 we tend to use Joda Time for more complex time calculations, especially when wanting to get time intervals. Java 8 has built-in options. Maybe take a glance at http://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances – Dan Armstrong Dec 10 '16 at 07:37
  • @DanArmstrong Yes I do. How do I get the Date object from the Timestamp string? – writzlpfrimpft Dec 10 '16 at 08:08
  • Look in http://stackoverflow.com/questions/1071800/how-to-use-joda-time-with-java-sql-timestamp for "Timestamp without Time Zone with Second Resolution Example". You'll find "DateTimeFormat.forPattern" which should get you where you need to be. – Dan Armstrong Dec 10 '16 at 08:13
  • @DanArmstrong yes it works, thank you – writzlpfrimpft Dec 10 '16 at 09:26
  • @DanArmstrong FYI, [that linked Answer](http://stackoverflow.com/a/19986708/642706) uses the Joda-Time library. The [Joda-Time](http://www.joda.org/joda-time/) project, now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), advises migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Dec 11 '16 at 01:50

2 Answers2

1

These issues have been discussed many times already on Stack Overflow. Yours is a duplicate of many other Questions. So search for details. Search for the class names seen below and for words such as elapsed.

Here is a brief nutshell answer.

ISO 8601

Convert your input string from SQL format to standard ISO 8601 format by replacing the SPACE in the middle with a T.

String input = "2016-12-11 14:26:35".replace( " " , "T" );

LocalDateTime

Parse as a LocalDateTime as this string lacks any indication of time zone or offset-from-UTC.

LocalDateTime ldt = LocalDateTime.parse( input );

ZonedDateTime

Apply the time zone intended as the meaning behind this string. Did you mean two in the afternoon of Auckland, Paris, or Montréal?

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdtThen = ldt.atZone( z );

Get the current moment. Again, the time zone is crucial. Cannot be ignored or wished away.

ZonedDateTime zdtNow = ZonedDateTime.now( z );

Difference?

As for "get the difference", you do not explain what that means.

If you want to represent a span of time in between as whole days, use Period.

Period p = Period.between( zdtThen.toLocalDate() , zdtNow.toLocalDate() );

If you want hour-minutes-seconds elapsed, use Duration.

Duration d = Duration.between( zdtThen , zdtNow );

To track as a pair of points in time, obtain the ThreeTen-Extra library, and use Interval class.

Interval interval = Interval.of( zdtThen.toInstant() , zdtNow.toInstant() );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

If I understand your question, you just have to convert your SQL timestamp to an Object Data. It's can be done with SimpleDateFormat.

See here https://www.mkyong.com/java/how-to-convert-string-to-date-java/

After that, you can make the difference between two date easilly with joda-time.

Cheers

thepaulo
  • 370
  • 4
  • 10