2

I am creating a method that will take a stored date and time from a DB which is stored as TEXT, use that date/time to then check between the current time and the current time plus one.

The issue I am having is checking between the two date and times because I have stored them all as Strings, I'm not sure how to check if it is between the hour:

private int mYear, mMonth, mDay, mHour, mMinute;
        listOfAllDateTimesToSendText = gameDetailsFromDatabase.agReturnDateTime();
                calendar = Calendar.getInstance();
                mYear = calendar.get(Calendar.YEAR);
                mMonth = calendar.get(Calendar.MONTH);
                mDay = calendar.get(Calendar.DAY_OF_MONTH);
                mHour = calendar.get(Calendar.HOUR_OF_DAY);
                mMinute = calendar.get(Calendar.MINUTE);
                String currentDateTime =  mYear + "-"+ mMonth + "-" + mDay + " " + mHour + ":" + mMinute + ":00";
                String currentDateTimePlusOneHour =  mYear + "-"+ mMonth + "-" + mDay + " " + mHour + 1 + ":" + mMinute + ":00";
                while(listOfAllDateTimesToSendText.moveToNext()){
                    String dateTimeFromCusor = listOfAllDateTimesToSendText.getString(listOfAllDateTimesToSendText.getColumnIndex(MainAutomateGames.AutomateGamesTable.COLUMN_DATE_TO_SEND_TEXT));
                    if(dateTimeFromCusor == currentDateTime || dateTimeFromCusor == currentDateTimePlusOneHour){

                    }

I have been looking around SO and the net but a lot of them using Date, or DateFormat, or yyyy-mm-dd etc and to be honest I am getting quite confused on which I should use for my current scenario.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Instead of using some crude own time implementation just use the [Date and Time API](http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html) by Java. `Date` and `Calendar` are outdated for quite some time now. – Ben Jun 22 '18 at 15:03
  • 3
    one step back: https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java – fantaghirocco Jun 22 '18 at 15:04
  • (a) Is the format of the dates in listOfAllDateTimesToSendText like "yyyy-MM-dd hh:mm:ss"? (b) are you checking the dates for equality or if the stored date is inbetween the 2 dates? –  Jun 22 '18 at 15:09
  • @mTak (a) - Yes it is - (B) if the stored date is between the 2 dates –  Jun 22 '18 at 15:21
  • Why do you use == operator and not < and >? –  Jun 22 '18 at 15:26
  • @mTak I will change it to < and > but since it is a string, I cannot get it to check it within the timeframe. I need to convert the strings into a date format but not sure what kind to use as there is so many –  Jun 22 '18 at 15:29
  • Yes you can. I will try to share a more detailed answer. –  Jun 22 '18 at 15:32

1 Answers1

1

If you're using a recent version of Java (prior to java 8), I would highly recomend that you take a look at the Joda Time library. Either way, think about doing these steps:

  • First, find out how to convert the string you're getting from the database to a java date format. Take a look here.

  • You don't need that Calendar Logic. It's bloated and you can just get an instance of the current day.

    DateTime today = new DateTime();

  • Find out how to add one hour to the current instance of today's date. Maybe something like this:

    DateTime plusOneHour = new DateTime(today.getMillis() + ~find out the amount~);

  • Get the three dates and compare what you want. If you research, you're gonna find out a lot of examples in how to compare two dates in java, using Joda or not.

If you don't figure out how to do some of these steps, we're here to help.

inafalcao
  • 1,415
  • 1
  • 11
  • 25
  • 1
    Since the OP is using Android, I don't think Java 8 is available. So, Joda Time is the only real option. Yes, it can be a pain to have to learn an outside library just solve a simple problem, but in this case Joda Time is the de-facto standard for time, and it's pretty easy to learn. – Bassinator Jun 22 '18 at 15:27