0

I am reading date (in variable "last_time") from a website in the format: MM-dd HH:mm, I want to get this date, parse it, which I did in this code. Then I want to know how much time passed, till now. I am trying to get the difference between the time read and the current time in milliseconds, but I am getting the wrong value when I compare it with Epoch time converters. Can I customly include the year and then parse it?

        Calendar date = Calendar.getInstance();
    date.setTime(new SimpleDateFormat("MM-dd HH:mm").parse(last_time)); // Parse into Date object
    date.set(Calendar.YEAR, 2022);
    var curr = date.getTimeInMillis();

    long now = Calendar.getInstance().getTimeInMillis(); // Get time now
    System.out.println(now);
    long differenceInMillis = now - curr;

Date used: 05-12 00:19 timeInmillisec obtained after parsing: 1652329140000 expected time in millisec: 1652415544

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 3
    Why not save yourself the headache, and use the "new" Java date/time API? – Robby Cornelissen May 12 '22 at 05:02
  • how will you get this format from "now"? @RobbyCornelissen – Mariam Baidauri May 12 '22 at 05:20
  • Can you give an example of a date for which the code above gives you the wrong answer? What answer are you expecting, and what answer are you getting? – Dawood ibn Kareem May 12 '22 at 05:31
  • my bad! unnecessary code removed @DawoodibnKareem – Mariam Baidauri May 12 '22 at 05:32
  • I updated the question, with date used, expected and obtained. @DawoodibnKareem – Mariam Baidauri May 12 '22 at 05:43
  • I recommend you don’t use `Calendar` and `SimpleDateFormat`. Those classes are poorly designed and long outdated, the latter in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatterBuilder`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 12 '22 at 06:23
  • Assuming your timezone is UTC+08:00, 1652329140000 is the correct answer. Why were you expecting something different? – Dawood ibn Kareem May 12 '22 at 06:32
  • 1
    In other words, your code is fine; but it's a bit more complex than it needs to be. The classes in the `java.time` package make this kind so much easier! – Dawood ibn Kareem May 12 '22 at 06:33
  • It would seem to me you made an error in your epoch time converter lookup. 1652415544 seoncds is for May ***13*** (tomorrow) at 00:19:04 UTC (04:19:04 AM at UTC offset -04:00). – Ole V.V. May 12 '22 at 06:34
  • (The discussion may be a bit easier if you tell us which time zone you’re in. I reproduced your result, 1652329140000, by running in America/Campo_Grande time zone, but there are many time zones at UTC offset -04:00.) – Ole V.V. May 12 '22 at 06:44
  • 1
    You are ignoring the crucial issue of time zone. Does your example data mean 19 minutes after midnight in Tokyo, or in Toulouse, or in Toledo? Those are different moments, several hours apart. See [my solution run live at Ideone.com](https://ideone.com/mNszun). Change the time zone to the one intended for your input. – Basil Bourque May 12 '22 at 06:51
  • Notice too that getting the current year requires a time zone as well. On New Year's Eve/New Year's Day, Tokyo may be in next year, while Toledo is simultaneously in last year. – Basil Bourque May 12 '22 at 06:58
  • 1
    Please watch your language. Profanity is distracting, possibly offensive, and may be confusing to the many readers here for whom English is a second language. – Basil Bourque May 12 '22 at 07:02

0 Answers0