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的。

  • 相关阅读:
    虚拟机安装配置
    大整数加法 面试题
    结构体
    操作文件
    Gcd HDU
    牛客练习赛47 A DongDong破密码 (异或性质,递推)
    ACM常用之 异或运算的性质。
    Wannafly挑战赛22 C 多项式(大数,多项式极限)
    大数全能模板
    Wannafly挑战赛22 D 整数序列 (线段树维护三角函数值)
  • 原文地址:https://www.cnblogs.com/AndyStudy/p/6039179.html
Copyright © 2011-2022 走看看