Java 判断时间是否在指定天数之内

时间:2023-03-09 05:38:42
Java 判断时间是否在指定天数之内
 import java.util.Date;
import java.text.SimpleDateFormat; public class WriteForBlog
{
static private int beforeDays = 8; // Use this vaule to judge the start date within 7 days.
static private long beforeSeconds = beforeDays * 24 * 60 * 60 * 1000; public static void main(String[] args)
{
String strTime = "2016-10-28";
if(isNewzt(strTime, beforeSeconds))
{
System.out.println("7 days old");
}
else
{
System.out.println("more than 7 days");
}
} private static boolean isNewzt(String theStrTime, long beforeDays)
{
boolean flag = false;
if("0".equals(theStrTime))
{
return flag;
} try
{
Date tDat = StrToDate(theStrTime, "");
long thm = tDat.getTime();
long chm=System.currentTimeMillis();
if(thm + beforeDays >=chm)
{
flag = true;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return flag;
} private static Date StrToDate(String str, String formatStr)
{
if (null == formatStr || "".equals(formatStr))
{
formatStr = "yyyy-MM-dd";
} SimpleDateFormat format = new SimpleDateFormat(formatStr);
Date date = null;
try
{
date = format.parse(str);
}
catch(Exception e)
{
e.printStackTrace();
}
return date;
} }

忘记是参考哪个blog的。