0

I am trying to compare the Time In or start time of the Subject and the current time. so for simple explanation, here's what I am trying to do..

if currentTime is beyond TimeIn, then I will toast a message "late", then if not beyond, then it will start a new activity.

enter image description here

in the image it shows the data and I am trying to get the pSubjtimeIn...

here is my code that I am trying...

String getSub = curSubj.getText().toString().trim();

dbref = FirebaseDatabase.getInstance().getReference("Subjects").child(getSub).child("pSubjtimeIn");

dbref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {

        if (snapshot.exists()) {
//I still got an error here Can't convert object of type java.lang.String to type            
SectionClass data = snapshot.getValue(SectionClass.class);
            if (data != null) {
                String getTime = data.getpSubjtimeIn();
                Toast.makeText(StudentHomePage.this, "" + getTime, Toast.LENGTH_SHORT).show();
                if (getTime != null) {
                    SimpleDateFormat dateFormat = new SimpleDateFormat("h:mm a", Locale.getDefault());
                    Date storedTime = null;
                    try {
                        storedTime = dateFormat.parse(getTime);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    if (storedTime != null) {
                        Calendar calendar = Calendar.getInstance();
                        Date currentTime = calendar.getTime();

                        calendar.setTime(storedTime);
                        calendar.add(Calendar.MINUTE, 15);
                        Date latetime = calendar.getTime();

                        if (currentTime.after(storedTime) && currentTime.before(latetime)) {
                            Toast.makeText(getApplicationContext(), "Late", Toast.LENGTH_SHORT).show();
                        } else {
                            Intent intent = new Intent(StudentHomePage.this, Present.class);
                            startActivity(intent);
                        }
                    }
                }
            }
        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    What's the value of `getSub` inside your reference? – Alex Mamo Mar 06 '23 at 12:01
  • 1
    Note: if you can, you should stop using `Calendar`, `Date` and `SimpleDateFormat`, since they are obsolete. Use classes from the `java.time` package. – MC Emperor Mar 06 '23 at 12:01
  • the value is the current subject, typically the MSYADD1. I get the value of that textView since it will be the basis if that subject is existing. – Venise Moises Joseph Mar 06 '23 at 12:02
  • 1
    1) do not use `Date`, `Calendar`, `SimpleDateTime`, ... all outdated by the `java.time` package. 2) this seems too complicated, getting a Date, then to Calendar, back to Data... maybe `LocalTime timeIn = LocalTime.parse(text, formatter); if (timeIn.isAfter(LocalTime.now)) { ... }` – user16320675 Mar 06 '23 at 12:02
  • how do I use the java.time package? – Venise Moises Joseph Mar 06 '23 at 12:04
  • *how do I use the java.time package?* (1) [Official Oreacle tutorial](https://docs.oracle.com/javase/tutorial/datetime/index.html) (2) If for older Android versions (before API 26/Oreo), through [core library desugaring](https://developer.android.com/studio/write/java8-support) – Ole V.V. Mar 06 '23 at 13:56
  • Did you search? This and similar questions have been asked and answered more than once before. – Ole V.V. Mar 06 '23 at 13:56

0 Answers0