Or, maybe you know a better approach how to solve this problem. I get dates in the format of "YYYY—MM-DD HH:MM:SS" and need to calculate time difference between now then in approximate increments: 1 minute ago, 1 hour ago, etc.
Any pointers much appreciated :)
First you need to parse your date string to a
Date
and then get the timestamp from that:Have a look at the
SimpleDateFormat
javadocs for information on which format string to use.Have a look at the SimpleDateFormat. You can define your pattern and parse it into a Java Date Object:
First line defines the SimpleDateFormat (can also be static if you reuse it a lot) Second line parses your input Third line converts it into milliseconds.
You need to convert your string into java.util.Date with the assistance of SimpleDateFormat (for examples see - https://stackoverflow.com/search?q=simpledateformat+%5Bjava%5D) and then get the number of milliseconds since January 1, 1970, 00:00:00 GMT.