Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的Environment.tickcount_Java_Jsp - Fatal编程技术网

java中的Environment.tickcount

java中的Environment.tickcount,java,jsp,Java,Jsp,这个C#代码在Java中的等价物是什么 int tick = 0; tick = Environment.TickCount; 在Java中没有标准的方法来保证系统的启动时间。如果您知道自己使用的是类Unix系统,则可以使用: Runtime.getRuntime().exec('uptime'); 或者,您可以读取系统文件: new Scanner(new FileInputStream("/proc/uptime")).next(); 在某些系统上,这也是由System.nanoTim

这个C#代码在Java中的等价物是什么

int tick = 0;
tick = Environment.TickCount;

在Java中没有标准的方法来保证系统的启动时间。如果您知道自己使用的是类Unix系统,则可以使用:

Runtime.getRuntime().exec('uptime');
或者,您可以读取系统文件:

new Scanner(new FileInputStream("/proc/uptime")).next();
在某些系统上,这也是由
System.nanoTime()
返回的值,但不能保证
System.nanoTime()
的来源(甚至返回值为正值)


如果您希望这样做的唯一原因是测量经过的时间,您可以使用
System.nanoTime()
,或者,如果您想测量经过的挂钟时间(包括在计时时可能进行的任何调整),请使用@TedHopp提到的
System.currentTimeMillis(),一种可能是使用System.currentTimeMillis()。在我的例子中,我想要的是以秒为单位的“滴答数”,而不是毫秒。下面是我目前用于相应C#方法的Java版本的内容


你到底想用这个达到什么目的
Environment.TickCount无论如何很少是一个有用的属性-我不知道有什么直接的等价物,但如果我们知道您试图使用它的目的,我们会有更好的机会帮助你。我需要得到系统启动后经过的毫秒数。apt代码用于什么java@SaranyaJON知道环境。TickCount给出了系统启动后经过的毫秒数。他在问你想用它做什么???@Saranya:正如Vikiii所说的-我的观点这很少是有用的信息,尤其是在web应用程序中。你为什么认为它真的有用?你想实现什么?我正在为一个会话获取UUID。在这里,我需要添加这个滴答数,通过添加这个,我将发送这个id和滴答数用于哈希算法。
   // Static field used by the tickCountInSeconds() method
   private static long _firstCallTimeSeconds = 0;
   /**
    * Method to get an arbitrary constantly increasing time in seconds, i.e., a time in seconds that
    * can be used to compare the relative times of two events, but without having any other meaning.
    *
    * The .Net version of this method uses the Windows "tick count" facility, but since that doesn't
    * exist in Java we fake it by getting the system (Unix-style) time in milliseconds and
    * converting it to seconds. But to avoid the "year 2038 problem" (or to avoid criticism for
    * creating a year 2038 vulnerability) the time of the first call is saved in a static field and
    * subtracted from the returned result.
    */
   private synchronized static int tickCountInSeconds() {
      long currentTimeSeconds = System.currentTimeMillis() / 1000L;
      if (_firstCallTimeSeconds == 0) {
         _firstCallTimeSeconds = currentTimeSeconds;
      }

      return (int)(currentTimeSeconds - _firstCallTimeSeconds);
   }