0

I'm trying to countdown to a time in the future (2024) and I have the time left from the users system to the exact date in 2024.

 String input = "Mon Apr 08 2024 18:18:29 UTC";
Date date = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH).parse(input);
long milliseconds = date.getTime();
long millisecondsFromNow = milliseconds - (new Date()).getTime();

That is my code, im trying to turn millisecondsFromNowinto a date a human can read, more specifically, years, months, days, hours, minutes, seconds and milliseconds left until the date.

Koji Ohara
  • 11
  • 5
  • What is exactly your problem? How can we help you? – theexiile1305 Aug 31 '18 at 05:57
  • 6
    Never, ever, ever perform simple mathematic arithmetic against time values, you are going to end up in a world of pain. Since Java 8, we've had the new date/time API, there's even a back port API which supports earlier versions of Java so there is simply no excuse any more – MadProgrammer Aug 31 '18 at 05:57
  • Do you want to have the difference between two points in time? Then have a look at: [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – LuCio Aug 31 '18 at 05:58
  • The classes you are using, `Date` and `SimpleDateFormat`, are long outdated and also poorly designed. I recommend you drop those and look into [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) for a replacement. You want `DateTimeFormatter`, `Period`, `Duration` and one or two more classes. There’s guidance in some of the answers to the linked questions. You may also look into `PeriodDuration` from the ThreeTen Extra library (use your search engine). – Ole V.V. Aug 31 '18 at 06:06
  • @M.Fuchs I have that code and `millisecondsFromNow` is a big, big number, so I'm trying to turn that number from milliseconds into the number of years, months, days, etc. left. What I need help with is just the converting of the milliseconds. Don't know if that was any clearer but I think these guys pointed me in the right direction – Koji Ohara Aug 31 '18 at 12:53

1 Answers1

-1

You can create a Duration object using the ofMillis method. Then have a look into this question for formatting options.

Michael
  • 2,443
  • 1
  • 21
  • 21