zoukankan      html  css  js  c++  java
  • 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的。

  • 相关阅读:
    [转载]使用uiautomator做UI测试
    [转载]Android相关开发网站
    [转载]Android开发必备的21个免费资源和工具
    c# List集合的Find方法适用
    c# GridView Footor列求合计
    c# List集合排序
    mysql中插入多条记录-微软批处理
    mysql中插入多条记录-微软批处理
    VS2005快捷键
    LinqToSql 小例子
  • 原文地址:https://www.cnblogs.com/AndyStudy/p/6039179.html
Copyright © 2011-2022 走看看