1

I'm creating an app for a coffee shop which should display information about whether it is closed or open in a given time. The app should display either "OPEN" or "CLOSED" as a TextView based on the time on user's device.

The coffee shop is open from 10:00:00 at morning and closed at 24:00:00 at night. Also, it is open from WED-SUN (MON and TUE closed).

For that, I created a TextView layout in XML with empty text field:

<TextView
    android:id="@+id/status_text_view"
    android:text=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

In java file I created a TextView element and referred to the TextView layout in the XML file:

TextView textView = (TextView) findViewById(R.id.status_text_view);

I also fetched date and time from user's device by using:

String currentDateTimeString = java.text.DateFormat.getDateTimeInstance().format(new Date());

From here I don't know how to do the final task of displaying either "OPEN" or "CLOSED" in the TextView layout based on date,time and day on the user's device. What I could do is only display the current date and time in the TextView layout by using the code:

textView.setText(currentDateTimeString);

Note : If you share any code, kindly add comments describing each line of code as I'm a beginner level programmer.

Zain
  • 37,492
  • 7
  • 60
  • 84
Pro
  • 13
  • 4

3 Answers3

0

You can make an if...else condition to set that TextView.

This is the example I think would help.

if((currentTime > openTime) && (currentTime < closeTime)) {     
   if((currentDay != dayMonday) && (currentDay != dayTuesday)) {
      textView.setText("OPEN");
   }
} else {
   textView.setText("CLOSED");
}

The first IF statement (if((currentTime > openTime) && (currentTime < closeTime))) to make sure the statement only returns TRUE if the currentTime variable is between 1000hrs to 2400hrs.

The second IF statement (if((currentDay != dayMonday) && (currentDay != dayTuesday))) is to make sure it return TRUE only if the currentDay is NOT MONDAY or TUESDAY.

And, your currentDateTimeString gives Date & the current Time together. So you have to parse it according to currentDay and currentTime or find suitable constants from Calendar class documentation.

Good luck. Hope it helps

  • Where and how do I specify the value for openTime, closeTime and weekdays? – Pro Jan 03 '21 at 16:50
  • 1
    @ChristianB suggestion to use JAVA SE *8 Date and TIME is actually good too, instead of Calendar class. It comes with specified member variables – kasijadi_lightnium Jan 03 '21 at 16:55
0

Java 8 introduces the java.time.* package which should be prefered! You can find more information in Java SE 8 Date and Time

You should consider using java.time.LocalDateTime.

The following function gives a simple example to decide if your shop is open:

import java.time.DayOfWeek;
import java.time.LocalDateTime;

boolean isOpen() {
   LocalDateTime now = LocalDateTime.now();

   // now.getHour/( returns the hour-of-day, from 0 to 23
   // so every hour that is greater equals 10 means open
   boolean isOpenByTime = now.getHour() >= 10;

   DayOfWeek dayOfWeek = now.getDayOfWeek();
   boolean isOpenByWeekDay = dayOfWeek != DayOfWeek.MONDAY && dayOfWeek != DayOfWeek.TUESDAY;

   return isOpenByTime && isOpenByWeekDay;
}

You can then use isOpen() like:

if(isOpen()) { textView.setText("OPEN"); } else { textView.setText("CLOSED"); }

Documentation

ChristianB
  • 2,452
  • 2
  • 10
  • 22
  • Pasting your code is throwing me a few errors even after doing necessary imports. Do I need to do anything with my Java installation , as you talked about some newer Java version ? I'm using Android Studio for linux with JDK embedded. – Pro Jan 03 '21 at 17:06
  • You need to have Java 8 – ChristianB Jan 03 '21 at 17:07
  • Hi, your code has one issue. In the fifth and sixth line, .hour and .dayOfWeek shows error. It says cannot resolve symbol 'hour', 'dayOfWeek', etc......... Can you please solve this issue? – Pro Jan 03 '21 at 18:45
  • I appologize, I originally wrote this in Kotlin, so they were issues with calling the functions. I fixed them. – ChristianB Jan 03 '21 at 19:01
0

I wrote your solution in JAVA Compiler, but logic will still work for android Just Implement the functions checkIfClosed(), getDay() and currentTime().

import java.util.*;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

public class checkIfclosed {

    public static void main(String[] args) throws IOException {

        try {
            System.out.println(checkIfClosed());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String checkIfClosed() throws ParseException {
        String out = "";
        if(!(getDay().equals("MONDAY") || getDay().equals("TUESDAY"))){

            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            if(dateFormat.parse(currentTime()).after(dateFormat.parse("10:00:00")) 
            && dateFormat.parse(currentTime()).before(dateFormat.parse("24:00:00"))){
                out = "OPEN";
            }
            else{
                out = "CLOSED";
            }
        }
        else{
            out = "CLOSED";
        }

        return out;
    }


    private static String getDay(){
        String ox = "";
        LocalDate localDate = LocalDate.now();
        DayOfWeek dayOfWeek = DayOfWeek.from(localDate);
        ox = dayOfWeek.name();
        return ox;
    }

    private static String currentTime(){
        LocalDateTime now = LocalDateTime.now();  
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        return dtf.format(now);
    }
    
}

Android Code

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            Log.v("isClosed", checkIfClosed());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


    private static String checkIfClosed() throws ParseException {
        String out = "";
        if(!(getDay().equals("MONDAY") || getDay().equals("TUESDAY"))){

            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            if(dateFormat.parse(currentTime()).after(dateFormat.parse("10:00:00"))
                    && dateFormat.parse(currentTime()).before(dateFormat.parse("24:00:00"))){
                out = "OPEN";
            }
            else{
                out = "CLOSED";
            }
        }
        else{
            out = "CLOSED";
        }

        return out;
    }


    private static String getDay(){
        String ox = "";
        LocalDate localDate = LocalDate.now();
        DayOfWeek dayOfWeek = DayOfWeek.from(localDate);
        ox = dayOfWeek.name();
        return ox;
    }

    private static String currentTime(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        return dtf.format(now);
    }
}
koshur
  • 124
  • 1
  • 9
  • Can you make it android studio specific please? – Pro Jan 03 '21 at 17:31
  • it will work on any device you just have to implement the methods in android studio. – koshur Jan 03 '21 at 17:37
  • hey check out I made an android specific implementation as you requested, the output in logcat is V/isClosed: OPEN – koshur Jan 03 '21 at 17:51
  • Hello, thanks for the response. The code is now error free. But the job is still not done. Can you please tell me how do I now display the status (either "OPEN" or "CLOSED") in a textview XML element. – Pro Jan 03 '21 at 18:08
  • Just use textView.setText(checkIfClosed()); – koshur Jan 03 '21 at 20:41