0

I have a long-variable which represents an amount of delay in milliseconds. I want to transform this long to some kind of Date where it says how many hours, minutes, seconds, days, months, years have passed.

When using Date toString() from Java, as in new Date(5).toString, it says 5 milliseconds have passed from 1970. I need it to say 5 milliseconds have passed, and 0 minutes, hours, ..., years.

Ad Fundum
  • 665
  • 6
  • 22
  • Well "how many months" have passed depends on your base point. If you have 30 days, is that 1 month and 2 days, or 1 month, or 0 months and 30 days? It all depends on your starting point. – Jon Skeet Apr 13 '14 at 14:57
  • I think [this question](http://stackoverflow.com/q/21667884/1343161) and its answers could help you. – Keppil Apr 13 '14 at 14:57
  • That's a fair point you have there... I guess I can write a method that transforms the amount of milliseconds in days, hours, minutes, seconds, and keeps counting days, even after 30-31. – Ad Fundum Apr 13 '14 at 15:00
  • Thanks Keppil, that looks promising! – Ad Fundum Apr 13 '14 at 15:01

3 Answers3

0

you cannot get direct values , without any reference date for your requirements, you need define first reference value like below:

String dateStart = "01/14/2012 09:29:58"; 
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
Date d1 = format.parse(dateStart);

the above is your reference date , now you need to find the current date and time using following.

long currentDateTime = System.currentTimeMillis();
Date currentDate = new Date(currentDateTime);
Date d2.format(currentDate)

and the difference of these values like long diff=d2-d1 will gives values in milliseconds. then

long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);

and similarly for months and years. you can also refer the example given on this link for more information http://javarevisited.blogspot.in/2012/12/how-to-convert-millisecond-to-date-in-java-example.html

Raju Sharma
  • 2,496
  • 3
  • 23
  • 41
  • Thanks for the explanation! As Keppil answered above, Joda-Time offers these and other methods, so I'm sticking with that. – Ad Fundum Apr 13 '14 at 15:51
0

From what I understand from your question you could achieve your goal by writing a method that will suit your needs i.e.:

static public String dateFromMili (long miliseconds) {
// constants that will hold the number of miliseconds 
  // in a given time unit (year, month etc.)
final int YEAR_IN_MILISECONDS = 12*30*24*60*60*1000;
final int MONTH_IN_MILISECONDS = 30*24*60*60*1000;
final int DAY_IN_MILISECONDS = 24*60*60*1000;
final int HOUR_IN_MILISECONDS = 60*60*1000;
final int MINUTE_IN_MILISECONDS = 60*1000;
final int SECONDS_IN_MILISECONDS = 1000;
// now use those constants to return an appropriate string.
return  miliseconds +" miliseconds, "
        +miliseconds/SECONDS_IN_MILISECONDS+" seconds, "
        +miliseconds/MINUTE_IN_MILISECONDS+" minutes, "
        +miliseconds/HOUR_IN_MILISECONDS+" hours, "
        +miliseconds/DAY_IN_MILISECONDS+" days, "
        +miliseconds/MONTH_IN_MILISECONDS+" months, "
        +miliseconds/YEAR_IN_MILISECONDS+" years have passed";

}

Than you will have to pas the number of miliseconds as a parameter to your new function that will return the desired String (i.e for two seconds):

dateFromMili (2000);

You could also print your answer:

System.out.println(dateFromMili(2000));

The result would look like this:

2000 miliseconds, 2 seconds, 0 minutes, 0 hours, 0 days, 0 months, 0 years have passed

Note that this method will return Strings with integer value (you will not get for example "2.222333 years" but "2 years"). Furthermore, it could be perfected by changing the noun from plural to singular, when the context is appropriate ("months" to "month").

I hope my answer helped.

0

This is how I solved the problem:

I used a library called Joda-Time (http://www.joda.org/joda-time/) (credits to Keppil!)

Joda-Time has various data-structures for Date and Time. You can represent a date and time by a DateTime-object. To represent the delay I was looking for, I had two options: a Period data-structure or a Duration data-structure. A good explanation of the difference between those two can be found here: Joda-Time: what's the difference between Period, Interval and Duration? .

I thus used a Duration-object, based on the current date of my DateTime-object. It has all the methods to convert the amount of milliseconds to years, months, weeks, days, hours, minutes and seconds.

Community
  • 1
  • 1
Ad Fundum
  • 665
  • 6
  • 22