0

I have variables which store the values

int day = Calender.SATURDAY;
int hour = 18;
int minutes = 33;

How can I convert this to Date? so How I can find the time difference in milliseconds from Current date time?

Cases :

  1. If the current date, time pass already passed, for example: If the current day is Saturday, and now time is 19: 00, than get the next week date time interval.

  2. If the current day is Saturday, and now time is 18: 30, (the time interval should be 180000 milliseconds = 3 minutes).

How can I do this in android? Please help me with finding the proper solution for this problem.

Dhiru Ard
  • 1
  • 2
  • Possible duplicate of [Android compare date](https://stackoverflow.com/questions/38356184/android-compare-date) – leonardkraemer Oct 20 '18 at 15:02
  • this is not duplicate , My question is Convert Day of week , hour , minute to date , than find diffreance , differance i can find i need to convert into date @leonardkraemer – Dhiru Ard Oct 20 '18 at 16:24

2 Answers2

0

Create a Calendar (Calendar.getInstance()), set your fields (cal.set(Calendar.DAY, day), etc.) and then call getTime() on it - that will return a Date.

BoD
  • 10,838
  • 6
  • 63
  • 59
0

For a proper solution I strongly advise to use the modern functions provided by java.time instead of the deprecated java.util. Read this post and Convert java.util.Date to what “java.time” type? to understand the package. How to get java.time on android pre API 26 is described here: How to use ThreeTenABP in Android Project

If you have read those you can solve your problem along the lines of this example:

public void dateStuff() {
    int day = DayOfWeek.SATURDAY.getValue();
    int hour = 18;
    int minutes = 33;
    LocalDateTime now = LocalDateTime.now();

    int dayDifference = now.getDayOfWeek().getValue() - day;
    LocalDateTime date = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth() - dayDifference, hour, minutes);

    long timeDifferenceNano = Duration.between(now, date).getNano();
    long timeDifference = TimeUnit.NANOSECONDS.toMillis(timeDifferenceNano);
    if (timeDifference > TimeUnit.MINUTES.toMillis(3)) {
        //do stuff
    }
}

Unfortunately I did not really understand when the two cases come into play, but I'm sure you can take it from here.

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54