0

I get a 10 digit timestamp from a json file and I've just figured out that this is Unix time in seconds and not in milliseconds.

So I went to my DateUtils class multiplied the timestamp in seconds with 1000, in order to convert it to timestamp in milliseconds.

When I tried to test isToday(), this line of code gave me a year like 50000 something...

int otherYear = this.calendar.get(Calendar.YEAR);

What is the mistake here?

DateUtils.java

public class DateUtils{

 public class DateUtils {
    private Calendar calendar;

    public DateUtils(long timeSeconds){
        long timeMilli = timeSeconds * 1000;
        this.calendar = Calendar.getInstance();
        this.calendar.setTimeInMillis(timeMilli*1000);
    }
    private boolean isToday(){
        Calendar today = Calendar.getInstance();
        today.setTimeInMillis(System.currentTimeMillis());

        // Todays date
        int todayYear = today.get(Calendar.YEAR);
        int todayMonth = today.get(Calendar.MONTH);
        int todayDay = today.get(Calendar.DAY_OF_MONTH);

        // Date to compare with today
        int otherYear = this.calendar.get(Calendar.YEAR);
        int otherMonth = this.calendar.get(Calendar.MONTH);
        int otherDay = this.calendar.get(Calendar.DAY_OF_MONTH);

        if (todayYear==otherYear && todayMonth==otherMonth && todayDay==otherDay){
            return true;
        }
        return false;
    }
}
Themelis
  • 4,048
  • 2
  • 21
  • 45

2 Answers2

2

The problem is in this block of code here:

    long timeMilli = timeSeconds * 1000;
    this.calendar = Calendar.getInstance();
    this.calendar.setTimeInMillis(timeMilli*1000);

You're multiplying the time by 1000 twice; remove one of the * 1000's and you should be good to go :)

Matthew Schlachter
  • 3,250
  • 1
  • 12
  • 26
0
public class DateUtils {
    private Instant inst;

    public DateUtils(long timeSeconds) {
        this.inst = Instant.ofEpochSecond(timeSeconds);
    }

    private boolean isToday() {
        ZoneId zone = ZoneId.systemDefault();

        // Todays date
        LocalDate today = LocalDate.now(zone);

        // Date to compare with today
        LocalDate otherDate = inst.atZone(zone).toLocalDate();

        return today.equals(otherDate);
    }
}

The other answer is correct. I am posting this to tell you that the Calendar class is long outdated and that its replacement in java.time, the modern Java date and time API, is so much nicer to work with and gives simpler and clearer code. As a detail, it accepts seconds since the Unix epoch, so you don’t need to multiply by 1000. No big deal, you may think, but one or the other reader might still have to think twice before understanding why you multiplied by 1000. They don’t need to now.

Depending on other requirements you may prefer to have your instance varibale be a ZonedDateTime rather than an Instant. In that case, just put the atZone call into the constructor instead of having it in the isToday method.

Link: Oracle Tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thank you very much for the information. I will check the time api. Nice that it accepts seconds since the Unix-epoch! – Themelis Mar 02 '18 at 22:01