0

I am working on Spring MVC project. It is an quasi online system where each client will install our system (Tomcat n Mysql will get installed through an installer) on their machine. They will download the data by connecting their machine to internet. Once data get downloaded they can disconnect from internet.

By considering above scenario, we want to validate the system date n time is correct according to time zone. I have checked How to get local time of different time zones?, The code :

java.util.TimeZone tz = java.util.TimeZone.getTimeZone("IST");
java.util.Calendar c = java.util.Calendar.getInstance(tz);

System.out.println(c.get(java.util.Calendar.HOUR_OF_DAY)+":"+c.get(java.util.Calendar.MINUTE)+":"+c.get(java.util.Calendar.SECOND));

Give the same time of the system. I want something which will tell time according to the time zone. Same as like when we set time zone on OS clock it will automatically set the correct date and time according to that time zone.

Implementation like this:

Date accurateTimeZoneDate = //HERE I WANT SOMETHING TO GET DATE ACCORDING TO TIME ZONE.
Date machineCurrentDate =  new Date();
if(accuratetimeZoneDate == machineCurrentDate)
{
  //machine date and time zone date is correct.
}
else
{
  //machine date and time zone date is NOT correct.
}

Update

We have tried this:

Daily it is mandatory to connect the system to internet so that application will ping to an central ntp and get the time and validate. once validation is successful then they can disconnect from internet. But in this case after validation they switch to some old date and use the expired content.

Community
  • 1
  • 1
Amogh
  • 4,453
  • 11
  • 45
  • 106
  • Can you try getting the timezone from the IP address allotted to the system? – Jayakrishnan Salim Jan 07 '16 at 05:46
  • @JayakrishnanSalim Oh! this seems new to me. Can you please elaborate more? And I am more concern to take date according to timezone and not current time zone of machine. – Amogh Jan 07 '16 at 05:48
  • So as far as I can figure out... your scenario is you're concerned the clock on the computer your software is running on might be wrong, and you want a way to figure out what the true time is? https://commons.apache.org/proper/commons-net/examples/ntp/NTPClient.java ? – Affe Jan 07 '16 at 05:49
  • @Affe I am alredy using this. Please read the problem we faced after using this in my question Update section. – Amogh Jan 07 '16 at 05:52
  • 1
    Right, so.. what else do you imagine you can do? If the clock is wrong it's wrong. There isn't some magical java API that can tell you, without calling another server, what the real time is if the system clock on the platform it's running on is incorrect. How would it ever know? Why don't you just set your program to refuse to run if the current system time is earlier than the last update from an authoritative source? – Affe Jan 07 '16 at 05:54
  • Possible duplicate of [How to get the current date and time of your timezone in Java?](http://stackoverflow.com/questions/1305350/how-to-get-the-current-date-and-time-of-your-timezone-in-java) – Lucky Jan 07 '16 at 05:55
  • @Affe, what you saying is true but as in OS clock when we set any time zone then OS will set the accurate time according to time zone (even if we are not connected to internet). So I just want to know is there any mechanism in java to do so. BTW `Why don't you just set your program to refuse to run if the current system time is earlier than the last update from an authoritative source` is better idea thanks for it. – Amogh Jan 07 '16 at 05:59
  • A 'java.util.Date' "according to timezone" is a nonsense. It's just a number of milliseconds since a fixed point. The java.util.Date for "11:33AM Jan 7 India Standard Time" is the exact same object as the java.util.Date for "11:03 PM Jan 6 US Mountain Time". It's not clear what you're trying to 'detect' by comparing them. "new Date()" always returns the exact same thing no matter what the operating system's timezone is set to. – Affe Jan 07 '16 at 06:04

1 Answers1

-3

Ok if you want system time and date then you should try new Date(). it will give current date time with timezone. Instead of using Calendar, you should use new Date()

System.out.println("Current Date And Time"+ new Date());

ojus kulkarni
  • 1,877
  • 3
  • 25
  • 41
  • 1
    I guess, you misunderstood. I don't want machine's date and time. To validate I want date and time according to time zone which I will check with current machine's date and time. – Amogh Jan 07 '16 at 05:38
  • Did you find the solution for your problem? – v.j May 17 '18 at 11:22
  • @v.j I am not sure about this but may be this [link](https://stackoverflow.com/a/24806352/4286411) might help you – ojus kulkarni May 18 '18 at 14:20