我想在我的Android应用程序中比较两次(时间戳)。 这是我的功能,检查当前时间是否在两个给定时间之间。
private boolean isBetween(String t1, String t2, String target){
boolean result = false;
try{
Date time1 = new SimpleDateFormat("HH:mm:ss").parse(t1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
Date time2 = new SimpleDateFormat("HH:mm:ss").parse(t2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
Date d = new SimpleDateFormat("HH:mm:ss").parse(target);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
result = true;
//checkes whether the current time is between 14:49:00 and 20:11:13.
//Toast.makeText(UserCheckout.this, "The time is between", Toast.LENGTH_SHORT).show();
}
}
catch (ParseException e){
result = false;
Toast.makeText(UserCheckout.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return result;
}
以下是我使用此功能的方法。
if (isBetween("19:00:00", "20:00:00", currentTime)){
Toast.makeText(this, "Time is between 7 and 8", Toast.LENGTH_SHORT).show();
}
if (isBetween("20:00:00", "21:00:00", currentTime)){
Toast.makeText(this, "Time is between 8 and 9", Toast.LENGTH_SHORT).show();
}
但我得到的结果都是真的。
答案 0 :(得分:2)
致电:
private boolean isBetween(String t1, String t2, String target){
boolean result = false;
try{
Date time1 = new SimpleDateFormat("HH:mm:ss").parse(t1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
Date time2 = new SimpleDateFormat("HH:mm:ss").parse(t2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
Date d = new SimpleDateFormat("HH:mm:ss").parse(target);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
result = true;
//checkes whether the current time is between 14:49:00 and 20:11:13.
//Toast.makeText(UserCheckout.this, "The time is between", Toast.LENGTH_SHORT).show();
}
}
catch (ParseException e){
result = false;
Toast.makeText(UserCheckout.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return result;
}
您要为这些日期添加一天。您正在检查您的“目标”日期(向前转换一天)是否在“19:00:00”和“20:00:00(明天)”和“20:00:00”和“21:00之间”: 00(明天)“
将您的代码更改为:
using LibaryData.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LibaryManagmentSystems.Models.Catalog
{
public class AssetDetailModel
{
public int AssetID { get; set; }
public string Title { get; set; }
public string AuthorOrDirector { get; set; }
public string Type { get; set; }
public int Year { get; set; }
public string ISBN { get; set; }
public string DeweyCallNumber { get; set; }
public string Status { get; set; }
public decimal Cost { get; set; }
public string CurrentLocation { get; set; }
public string ImageURL { get; set; }
public int PatronName { get; set; }
public Checkout LatestChechout { get; set; }
public IEnumerable<CheckoutHistory> CheckoutHistory { get; set; }
public IEnumerable<AssetHoldModel> CurrentHolds { get; set; }
}
public class AssetHoldModel
{
public string PatronName { get; set; }
public string HoldPlace { get; set; }
}
}