提问人:Venise Moises Joseph 提问时间:3/6/2023 最后编辑:Frank van PuffelenVenise Moises Joseph 更新时间:3/7/2023 访问量:57
将时间与当前时间进行比较 [重复]
Compare time in to current time [duplicate]
问:
我正在尝试将主题的时间或开始时间与当前时间进行比较。所以为了简单的解释,这是我想做的。
如果 currentTime 超出 TimeIn,那么我将“late”祝酒,如果不是超出,那么它将启动一个新活动。
在图像中它显示了数据,我正在尝试获取 pSubjtimeIn...
这是我正在尝试的代码......
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);
}
}
}
}
}
答: 暂无答案
评论
getSub
Calendar
Date
SimpleDateFormat
java.time
Date
Calendar
SimpleDateTime
java.time
LocalTime timeIn = LocalTime.parse(text, formatter); if (timeIn.isAfter(LocalTime.now)) { ... }