1

I am trying to pass a string of the form 12:00 into milliseconds based on the current date but I seem unable to get a good understanding of how the Calendar and Date class work to achieve this.

Now I have this code:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

Calendar startTime = Calendar.getInstance();
String alarmPref = preferences.getString(PreferenceUtility.getReminderTimes(context), "12:00");
SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
Date date = format.parse(alarmPref);
startTime.setTime(date);

This unfortunaltely gives me when logged like this:

Log.d(TAG, "Time to start:" + futureTime);
Log.d(TAG, "Date: " + date);

Gives the following results:

07-25 14:45:21.057 8409-8409/com.google.developer.bugmaster D/PreferenceUtility:Time PreferenceUtility: 21:20
07-25 14:45:21.057 8409-8409/com.google.developer.bugmaster D/QuizJobScheduler: Time to start:126000
Date: Thu Jan 01 12:00:00 GMT+01:00 1970

As seen the required string is 21:20 ( as expected ) but Time to start remains at the value 126000 and hence I keep getting the date to be Date: Thu Jan 01 12:00:00 GMT+01:00 1970, which of course is a reference to the epoch date and time.

How can I get a reference date and time that refers to the time of 21:20 and for the current date the app is running. Forgive me for my ignorance as I have tried so many literature with no success most likely I am unable to understand them.

George Udosen
  • 906
  • 1
  • 13
  • 28
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jul 26 '18 at 09:34
  • Possible duplicate of [Alarm Manager broadcasts unexpectedly](https://stackoverflow.com/questions/49661237/alarm-manager-broadcasts-unexpectedly) – Ole V.V. Jul 26 '18 at 09:41
  • A link to good examples with reference to my query would be nice – George Udosen Jul 26 '18 at 11:36
  • Didn’t I just provide that? https://stackoverflow.com/a/49675035/5772882 – Ole V.V. Jul 26 '18 at 11:38
  • My apologies for not seeing that! – George Udosen Jul 26 '18 at 11:40

2 Answers2

1

Use this code:

    String time = "21:20";
    String[] splittedTime = time.split(":");

    Calendar calendar = Calendar.getInstance();

    // Set the current date and time
    calendar.setTime(new Date());

    // Set the desired hour and minute
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(splittedTime[0]));
    calendar.set(Calendar.MINUTE, Integer.parseInt(splittedTime[1]));

    // Clear seconds and milliseconds
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    // the variable date will get today's date and the desired time
    Date date = calendar.getTime();

I hope you understand my comments in the code

0

Java's Date class does not provide the means to set ONLY the time while using the current calendar day. Android's Calendar class does.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 21);
calendar.set(Calendar.MINUTE, 20);

This above example requires you to parse the time without a SimpleDateFormat. Splitting the string in half (using the ':') and integer parsing the two strings would give you the values to put in.

TEK292
  • 261
  • 1
  • 13