Android课程签到时间与当前时间对比及类型转换报错问题
问题描述
我需要对比课程签到时间(Time In)与当前时间,逻辑规则为:若当前时间晚于签到时间则弹出“late”提示,否则启动新的Activity。但从Firebase Database获取pSubjtimeIn数据时,触发了无法将java.lang.String类型转换为SectionClass类型的错误,相关代码如下:
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); } } } } } } // 原代码遗漏了onCancelled方法 @Override public void onCancelled(@NonNull DatabaseError error) { // 未实现 } });
错误原因
你的数据库引用直接指向了pSubjtimeIn节点,该节点存储的是字符串类型的时间值,而非SectionClass对象。但你尝试将这个字符串节点转换成SectionClass类型,这就导致了类型不匹配的错误。
修复方案
直接从DataSnapshot中获取字符串类型的时间值,无需转换成SectionClass,修改后的代码如下:
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()) { // 直接获取字符串类型的签到时间 String getTime = snapshot.getValue(String.class); 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(); Toast.makeText(StudentHomePage.this, "时间格式解析失败", Toast.LENGTH_SHORT).show(); return; } 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); } } } } } @Override public void onCancelled(@NonNull DatabaseError error) { // 处理数据库操作失败的情况 Toast.makeText(StudentHomePage.this, "获取签到时间失败: " + error.getMessage(), Toast.LENGTH_SHORT).show(); } });
额外说明
- 补充实现了
onCancelled方法,用于处理数据库操作取消或失败的场景,避免异常遗漏。 - 增加了时间解析失败的提示,方便排查格式不匹配的问题。
- 确保Firebase数据库中
pSubjtimeIn节点的时间格式与代码中SimpleDateFormat指定的"h:mm a"完全一致(例如"10:15 AM"),否则会触发解析异常。
内容的提问来源于stack exchange,提问作者Venise Moises Joseph
相关产品推荐
相关产品推荐

