Java 判断时间是否在指定天数之内
1 import java.util.Date;2 import java.text.SimpleDateFormat;
3
4
5 public class WriteForBlog
6 {
7 static private int beforeDays = 8; // Use this vaule to judge the start date within 7 days.
8 static private long beforeSeconds = beforeDays * 24 * 60 * 60 * 1000;
9
10 public static void main(String[] args)
11 {
12 String strTime = "2016-10-28";
13 if(isNewzt(strTime, beforeSeconds))
14 {
15 System.out.println("7 days old");
16 }
17 else
18 {
19 System.out.println("more than 7 days");
20 }
21 }
22
23 private static boolean isNewzt(String theStrTime, long beforeDays)
24 {
25 boolean flag = false;
26 if("0".equals(theStrTime))
27 {
28 return flag;
29 }
30
31 try
32 {
33 Date tDat = StrToDate(theStrTime, "");
34 long thm = tDat.getTime();
35 long chm=System.currentTimeMillis();
36 if(thm + beforeDays >=chm)
37 {
38 flag = true;
39 }
40 }
41 catch(Exception e)
42 {
43 e.printStackTrace();
44 }
45 return flag;
46 }
47
48 private static Date StrToDate(String str, String formatStr)
49 {
50 if (null == formatStr || "".equals(formatStr))
51 {
52 formatStr = "yyyy-MM-dd";
53 }
54
55 SimpleDateFormat format = new SimpleDateFormat(formatStr);
56 Date date = null;
57 try
58 {
59 date = format.parse(str);
60 }
61 catch(Exception e)
62 {
63 e.printStackTrace();
64 }
65 return date;
66 }
67
68 }
忘记是参考哪个blog的。
以上是 Java 判断时间是否在指定天数之内 的全部内容, 来源链接: utcz.com/z/393771.html