0

`

SimpleDateFormat curFormater = new SimpleDateFormat("yyyyMMdd");
                        Date c = Calendar.getInstance().getTime();
                        String formattedDate=curFormater.format(c);
                        Log.d("tag","formate: " + formattedDate);
                   imageDate=dataSnapshot.getValue(String.class);
                        Log.d("date","date: " + imageDate);
                        if (imageDate==formattedDate) {
                            Toast.makeText(accountantFunctions.this, "only 1 image upload is allowed per day", Toast.LENGTH_LONG).show();
                            cameraBtn.setEnabled(false);
                        }else{
                            askCameraPermissions();
                        }

`

 The logcat is showing this:
    D/ColorViewRootUtil: nav gesture mode swipeFromBottom ignore false downY 1564 mScreenHeight 2340 mScreenWidth 1080 mStatusBarHeight 54 globalScale 1.125 nav mode 3 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=407.0, y[0]=1564.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=260828295, downTime=260828295, deviceId=4, source=0x1002, displayId=0 } rotation 0
    D/tag: key: vikashbhushan189@gmail.com
    D/date: date: https://accounting-book-5401b.firebaseio.com/Accountantuser/vikashbhushan189%40gmail%2Ccom/timestamp
    D/tag: formate: 20200229
    D/date: date: 20200228

Here I am getting imageDate from firebase realtime database. Whenever i run if condition always remains false, and askCameraPermissions(); is called up ever time.

user2182857
  • 718
  • 8
  • 20

2 Answers2

1

When comparing, always keep that in mind.

== is used for Reference Equality.

.equals() is used to check if contents of both objects are Equal/Same

In your case as you are comparing contents, you should be using:

if (imageDate.equals(formattedDate)) {
    Toast.makeText(accountantFunctions.this, "only 1 image upload is allowed per day", Toast.LENGTH_LONG).show();
    cameraBtn.setEnabled(false);
} else {
    askCameraPermissions();
} 

For more understanding between == vs .equals() , please have a look to this answer == vs .equals()

Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
0

To compare String type you need to use equals() method

Solution -

if (imageDate.equals(formattedDate)) {
}
Roman
  • 2,464
  • 2
  • 17
  • 21