0

Hi i got a data that is scan from the QR code with contain DateTime . but now i want to make a validation where , the code will proceed to another activity when its scan only date and time . I post the code here

 Log.i("<<<<<<Asset Code>>>>> ",
                        "<<<<Bar Code>>> " + sym.getData());
                scanResult = sym.getData().trim();


                SimpleDateFormat dateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

                if(scanResult.equals(dateFormat))
                {
                    new createClockIn().execute();
                }

                else {
                    Toast.makeText(BarcodeScanner.this, "Sorry this is not a valid date and time",  Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(getApplicationContext(), BarcodeScanner.class);
                    intent.putExtra("username", staffIDStr1);
                    startActivity(intent);

                }

but this is seems to be wrong when im scan the qr code with the date and time , its pops out the toast. The data should be inserted to the database like this

enter image description here

Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37

2 Answers2

0

You have to parse the ScanResult-String into a Date-Object. This is done by using parse()-method of your SimpleDateFormatter. Equals does not work here!

Try to parse your ScanResult:

SimpleDateFormat dateFormat =  new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");    
try {
    final Date dateResult = dateFormat.parse(scanResult);
    SimpleDateFormat outFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    final String formattedForDatabase = outFormat.format(dateResult);
    // TODO Do your stuff.
    new createClockIn().execute();
} catch (ParseException e) {
    Toast.makeText(BarcodeScanner.this, "Sorry this is not a valid date and time",  Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(getApplicationContext(), BarcodeScanner.class);
    intent.putExtra("username", staffIDStr1);
    startActivity(intent);
}
Christopher
  • 9,682
  • 7
  • 47
  • 76
0

Handle Exception like this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try {
        sdf.parse(scanResult);
} catch (ParseException e) {
    // Invalid Date
    e.printStackTrace();
}
Amy
  • 4,034
  • 1
  • 20
  • 34