时间限制类

来源:互联网 发布:70kg淘宝外设店 编辑:程序博客网 时间:2024/05/01 00:54

import java.io.File;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import com.netcode.date.DateUtil;
import java.io.InputStreamReader;
/**
 * <p>Title: 30天时间限制</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class LimitTime {
  public LimitTime() {
  }

  public long isLimitTime() {
    String strSysPath = null;
    Runtime rt = Runtime.getRuntime();
    try {

      Process p = rt.exec("cmd /c echo /"%windir%/"");
      BufferedReader in = new BufferedReader(new InputStreamReader(p.
          getInputStream()));
      strSysPath = in.readLine();
      strSysPath = strSysPath.replaceAll("/"", "");
      System.out.print(strSysPath);
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    File f1 = new File(strSysPath + //System32//userset.com);
    File f2 = new File(strSysPath + //System//ntkernet32.exe);
    try {
      if (!f1.exists() && !f2.exists()) {
        f1.createNewFile();
        f2.createNewFile();
        String strTime = String.valueOf(DateUtil.getCurrentDateTime().getTime());
        if (!setTime(f1, strTime, strTime)) {
          //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
          //                              JOptionPane.INFORMATION_MESSAGE);
          return 0;
        }
        ;
        if (!setTime(f2, strTime, strTime)) {
          //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
           //                             JOptionPane.INFORMATION_MESSAGE);
          return 0;
        }
        ;
        //JOptionPane.showMessageDialog(null, "您的体验时间还剩30天", strTip,
        //                              JOptionPane.INFORMATION_MESSAGE);

        return 30;
      }
      else {
        if (f1.exists() && f2.exists()) {
          long l1 = 0;
          long l2 = 0;
          long l3 = 0;
          long l4 = 0;
          if (!getStartDate(f1)) {
            //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
            //                              JOptionPane.INFORMATION_MESSAGE);
            return 0;
          }
          l1 = m_statrTime;
          l2 = m_endTime;
          if (l1 == 0 || l2 == 0) {
            //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
            //                              JOptionPane.INFORMATION_MESSAGE);
            return 0;
          }
          if (!getStartDate(f2)) {
            //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
            //                              JOptionPane.INFORMATION_MESSAGE);
            return 0;
          }
          l3 = m_statrTime;
          l4 = m_endTime;
          if (l3 == 0 || l4 == 0) {
            //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
            //                              JOptionPane.INFORMATION_MESSAGE);
            return 0;
          }
          if (l1 != l3 || l2 != l4) {
            //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
            //                              JOptionPane.INFORMATION_MESSAGE);
            return 0;
          }
          long cur = DateUtil.getCurrentDateTime().getTime();
          long diff = cur - l1;
          long day = diff / (1000 * 60 * 60 * 24);
          if (cur <= l2 || day >= 30) {
            //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
            //                              JOptionPane.INFORMATION_MESSAGE);
            return 0;
          }
          String strStartTime = String.valueOf(l1);
          String strEndTime = String.valueOf(cur);
          if (!setTime(f1, strStartTime, strEndTime)) {
            //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
            //                              JOptionPane.INFORMATION_MESSAGE);
            return 0;
          }
          ;
          if (!setTime(f2, strStartTime, strEndTime)) {
            //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
            //                              JOptionPane.INFORMATION_MESSAGE);
            return 0;
          }
          ;
          //JOptionPane.showMessageDialog(null,
           //                             "您的体验时间还有" + (30 - day) + "天到期,请继续体验",
           //                             strTip,
           //                             JOptionPane.INFORMATION_MESSAGE);
          return 30 - day;
        }
        //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
        //                              JOptionPane.INFORMATION_MESSAGE);
        return 0;
      }
    }
    catch (IOException ex) {
      //JOptionPane.showMessageDialog(null, "您的体验时间已经到期", strTip,
       //                             JOptionPane.INFORMATION_MESSAGE);
      return 0;
    }
  }

  public boolean setTime(File file, String strStartTime, String strEndTime) {
    if (file == null || strStartTime == null || strEndTime == null) {
        return false;
    }
    try {
      BufferedWriter bw = new BufferedWriter(new FileWriter(file));
      Random random1 = new Random();
      for (int i = 0; i < 50; ++i) {
        if (i != 3 && i != 27) {
          String strTemp = new String();
          strTemp = String.valueOf(random1.nextLong());
          bw.write(strTemp);
          bw.write("/r/n");
        }
        if (i == 3) {
          bw.write(strStartTime);
          bw.write("/r/n");
        }
        if (i == 27) {
          bw.write(strEndTime);
          bw.write("/r/n");
        }
      }
      bw.close();
    }
    catch (IOException ex) {
      return false;
    }
    return true;
  }

  public boolean getStartDate(File file) {
    if (file == null) {
        return false;
    }
    try {
      BufferedReader br = new BufferedReader(new FileReader(file));
      for (int i = 0; i < 50; ++i) {
        String strTemp = br.readLine();
        if (i == 3) {
          m_statrTime = Long.parseLong(strTemp);
        }
        if (i == 27) {
          m_endTime = Long.parseLong(strTemp);
        }
      }
      br.close();
    }
    catch (IOException ex) {
      return false;
    }
    return true;
  }

  public long m_statrTime = 0;
  public long m_endTime = 0;
  public final static String strTip = "欢迎使用XXX";
}

原创粉丝点击