当前日期与指定日期的差值
public static String timeInterval(String date)
{
Date date1 = new Date();
Date date2 = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
date2 = sdf.parse(date);
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
long time = date2.getTime() - date1.getTime();
long day = time / (24 * 60 * 60 * 1000);
long hour = time / (60 * 60 * 1000) % 24;
long min = time / (60 * 1000) % 60;
long sec = time / 1000 % 60;
return day + "," + hour + "," + min + "," + sec;
}
比较时间大小
public static int compare_date(String DATE1, String DATE2, String format)
{
DateFormat df = new SimpleDateFormat(format);
try
{
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() > dt2.getTime())
{
return 1;
}
else if (dt1.getTime() < dt2.getTime())
{
return -1;
}
else
{
return 0;
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
return 0;
}
两个时间段的交际(例:goods商品可售时间,shop商家可售时间)
public static String timeAll(String goods, String shop)
{
String time = null;
String[] shoptimeAll = shop.split(",");// 所有的商家营业时间
String[] goodstime = goods.split("--");// 将商品时间分开
for (int i = 0; i < shoptimeAll.length; i++)
{
String m = null;
String[] shoptime = shoptimeAll[i].split("--");
Integer is0 = compare_date(goodstime[0], shoptime[0], "HH:mm");
Integer is1 = compare_date(goodstime[0], shoptime[1], "HH:mm");
Integer is2 = compare_date(goodstime[1], shoptime[0], "HH:mm");
Integer is3 = compare_date(goodstime[1], shoptime[1], "HH:mm");
if ((is0 == 1 || is0 == 0) && (is1 == -1 || is1 == 0) && (is3 == 1 || is3 == 0))
{
time = goodstime[0] + "--" + shoptime[1];
}
if ((is0 == 1 || is0 == 0) && (is1 == -1 || is1 == 0) && (is3 == -1 || is3 == 0))
{
time = goodstime[0] + "--" + goodstime[1];
}
if ((is0 == -1 || is0 == 0) && (is1 == -1 || is1 == 0) && (is3 == -1 || is3 == 0) && (is2 == 1 || is2 == 0))
{
time = shoptime[0] + "--" + goodstime[1];
}
if ((is0 == -1 || is0 == 0) && (is1 == -1 || is1 == 0) && (is3 == 1 || is3 == 0) && (is2 == 1 || is2 == 0))
{
time = shoptime[0] + "--" + shoptime[1];
}
}
return time;
}
多时间段取交集goods单时间段(9:00--10:00)shop多时间段(3:00--9:00,10:00--19:00)
public static String time(String goods, String shop)
{
String time = null;
String[] shoptimeAll = shop.split(",");
for (int i = 0; i < shoptimeAll.length; i++)
{
String b = null;
if (time == null)
{
time = timeAll(goods, shoptimeAll[i]);
}
else
{
b = timeAll(goods, shoptimeAll[i]);
}
if (time != null && b != null)
{
time = time + "," + b;
}
}
return time;
}