views:

84

answers:

4

I want to know the current Date and Time.

The code

Calendar.getInstance();

represents a date and time of the system on which the program is running and the system date can be wrong.

So Is there any way by which I can get correct current date and time irrespective of the date and time of the system on which program is running?

A: 

The programming level methods are developed to Get Date and Time from your system itself. You cannot modify them to get the dates except than the System specified.

For your additional requirement, if you wish to really have it, you would require a Synchronization between your Client Machine and Server.

Mr.Expert
+2  A: 

If you're on the internet, you might be able to ask a known and trusted time source. If the person running your program wants to prevent your program from doing that (like if you've given them a time limited license and they don't want to pay for more time), they might spoof or block that connection.

On one project I was on, we placed a secure, trusted time source in the hardware that could not be tampered with. It was designed for encryption and licensing, and had a Java library to access it. Sorry, I can't remember the name of the device.

So the answer is maybe yes, maybe no.

Paul Tomblin
+3  A: 

Have your system Internet access? If so, you can use synchronization with precise time services (for example: http://tldp.org/HOWTO/TimePrecision-HOWTO/ntp.html) and grant that you want.

Dewfy
+2  A: 

I don't understand completely your question but I can answer your title:

GregorianCalendar gc = new GregorianCalendar(System.getCurrentTimeMillis());
int year = gc.get(Calendar.YEAR);
Martijn Courteaux
@Martin If you don't understand the question then I don't know how to explain it more. Better to leave without answering if you don't understand it. lol
Yatendra Goel
@Yatendra - don't be snarky with people who are trying to help you.
Stephen C