Fetching JVM timezone on Linux Box - java

Is it possible to get the TIMEZONE set in JVM using a command in Linux? I am trying to fetch this information from a production box where I wouldn't able to deploy any code to check.

JVM makes some statistics about the app available via JMX, and some are available by default, so it's rather likely you will be able to retrieve them from your app, too. You can use jconsole tool to connect to a Java app and read those values. I don't see the clock time being one of them, but you can check the application's startup time, and the uptime. Adding one to the other gives you the current time as the app sees it. You can find both values in jconsole in the "MBeans" tab: in the tree on the left select: java.lang / Runtime / Attributes - they will be called "Uptime" and "StartTime". Uptime is in milliseconds and StartTime is in milliseconds since January 1st, 1970.
PS: If the above doesn't work, you can try to retrieve the time indirectly. The time returned by System.currentTimeMillis() is based on the machine's clock, so it should be the same as returned by any other program that queries the clock, e.g. the command-line date command. One possible deviation would be the possibility of the java application using a different time zone, e.g. due to environment variables set differently than for your command-line program.

no. this is not possible. you cannot get the time set in JVM using a command in linux. for further information run "java" command in command prompt or console and you will see which commands JVM can receive directly

Related

Get time diff between windows and linux machine using java or any other API

I have a code running on Windows and Linux machine.
Message will be sent from windows to Linux machine and we include sent_time on message.
Linux machine will receive the message and we stamp receive_time on message.
Both the time are calculated using standard java API System.currentTimeMillis().
Problem here is, receive_time is ahead of sent_time which is not right as message was sent first and then received. This mostly looks to be clock sync issue between Windows and Linux machine.
How can we get the clock diff between Windows and Linux machine? (Not sure how to run program on both the machines and claculate time diff which is in millis)
if there is any sync issue, how can we get and add delta time receive_time ?
Please note that all the libraries are same on both machines.
You are trying to find time deviations, maybe even synchronize clocks across the network. This adds complexity because neither of the two machines know how much lag is in the network passing the data.
The NTP protocol mitigates all this. I'd use an existing implementation. But if you do not want to synchronize the system clock but still understand how it is done, check out the protocol documentation: http://www.ntp.org/rfc.html

Determine the time the computer was turned ON using Java [duplicate]

This question already has answers here:
Get system uptime in Java
(6 answers)
Closed 6 years ago.
I have a Java JAR program that is run on Windows startup. It needs to register the time the computer was turned on, so naturally it simply gets the time when the main method is called. Works fine.
But, some of the computers we installed the program in are very (very) slow. It is unsurprising that the startup of the program may occur after several minutes upon turning on the computer.
An inaccuracy of 10-15 seconds is acceptable (our newest computers). But two or three minutes in the oldest ones not quite so.
Is there a reasonable way to determine the time the computer was turned ON? Using Java?
I do realize that for this kind of job, Java was a terrible choice. It is well known that Java startup is not particularly fast at all. I'll ask this anyway since the program is already done with Java - but if there is no solution I'll work on a C++ version.
Windows.
What about write short *.bat file, with only one functionality - after start it writes actual time to a file.
When your Java program is loaded, it just first try to read file and find out the time of computer start.
Maybe even better idea - start your program through .bat file. First get actual time and save it to variable, then start program with value of this variable as parameter. When the program is on, it will first recieve the time of start in main method.
There are several options to find this out on Windows:
systeminfo | find /i "Boot Time"
(As noted by #Robert this only works on English Windows installations)
or
wmic os get lastbootuptime
or
net statistics workstation
Just use Runtime.getRuntime().exec("command"); to call the command.
You can try launching from within Java (using Runtime.getRuntime().exec(...)) a command that will give you the exact machine start time (systeminfo) or uptime (there is a Microsoft utility uptime.exe).
This is not a java related question.
It is a question operating system related.
If your operating system has a function that gives you this information you can call that function
The code to call an os function from java is
Runtime.getRuntime().exec("command");
In linux the commandline command to get the information of the last boot is the following:
last reboot | head -1
As I said it depends from operating system to operating system.
For a windows environment you can use the following code:
systeminfo | findstr /C:"System Boot Time"

Run servers in one machine in different timezone

I am running Three Quartz servers(as java wrapper services) from one Linux Virtual Server machine. My requirement is to run these servers in different time zone in one machine. e.g. Say there are three servers name A, B and C then A should schedule job based on Central Time zone, B should schedule jobs based on Eastern Time zone and so on. Is there any way we could achieve this?
Time zones are but concepts in Unix land. Each system clock should run with UTC, calculating displayed times based on the timezone you configure within the system.
Depending on which software should deal with that, it might be totally sufficient to set the TZ environment variable correctly.
I created three users for three different servers and set the desired time zones in .bashrc file. Say user name is user1 then do the following:
Open file /home/user1/.bashrc usinf any editor.
Modify and enter this line:
export TZ="/usr/share/zoneinfo/{TIMEZONE-DIRECTORY}/{TIMEZONE_FILE}"
Save the file.
Timezone is set for the user now.

How to measure time when being logged in

I want to implement a project where I check system timings whenever I am logged in.
If I enter the office and log in my system the I should get the time and also when I go for a break I just lock my PC and go so at that time time should stop and again when I login it should start.
Basically it should show me the total time I was logged in my computer/PC.
In java you can obtain the time by several ways. Two of them are :
instanciate a new Date object :
Date myDate = new Date() ;
using :
System.currentTimeMillis()
I think you should think of using a software dedicated to that instead of implementing this in Java.
What you could do is this:
Create a Java application that can
log the time somewhere, using the
System.currentTimeMillis() or new
Date() approach.
Create a scheduled task in windows
that runs each time the user locks,
unlocks, logs on and logs off the
system. This scheduled task should
run your application. It should be
as simple as calling a batch file
which in turn invokes your Java
application.
The application should use all the
times captured to calculate the
effective time.
You can use System.currentTimeMillis() (or System.nanoTime()) for the start and the end, and then calculate the difference.
This will give you the time when you start / close Java. Linking this to system startup / system shutdown can be done by launching the application on startup. But that depends on the OS.

What does -XX:+UseGetTimeOfDay as a parameter do?

Whilst working on a Java application I've come across the -XX:+UseGetTimeOfDay parameter on a java command, I can't find much information about it.
Firstly, what does the -XX mean, and what would the UseGetTimeOfDay provide? I haven't seen these before and I'm wondering what they do, could it be something specific to my application or are these standard Java parameters?
I don't know if it's still relevant now, but I found some documentation on it:
Instructs the JVM to use the GetTimeOfDay call instead of the mechanism used in earlier versions whereby the number of cpu ticks since the application started is used to calculate the current time. With this new mechanism, changes to the system date or time using date(1), adjtime(2), or time synchronization utilities such as ntp are not reflected in the date and time that Java™ returns, until the process is restarted. If your application requires that Java™ immediately reflects such system time changes, you can use the -XX:+UseGetTimeOfDay option, however you may notice a drop in performance.
In general, -X and -XX flags are ones which control the JVM (and are VM-specific).
From the HotSpot options page (which doesn't list this one):
Options that begin with -X are non-standard (not guaranteed to be supported on all VM implementations), and are subject to change without notice in subsequent releases of the JDK.
Options that are specified with -XX are not stable and are not recommended for casual use. These options are subject to change without notice.
Found this thread which says:
The HotSpot JVM uses the
gettimeofday() system call to obtain
date and time information.
For performance reasons a new
mechanism is used that uses the number
of CPU ticks since the application
started, to calculate the current
time. As a result, changes to the
system date or time using date(1),
adjtime(2) or time synchronization
utilities such as ntp will not be
reflected in the date and time that
the Java program returns, until the
process is restarted.
If your application requires that
system time changes are immediately
reflected, you can use the
-XX:+UseGetTimeOfDay option to tell the JVM to use the gettimeofday call
instead of the new, lightweight
mechanism. However you may notice a
drop in performance.

Categories

Resources