Solved

java and downloading time

Posted on 1998-08-27
4
171 Views
Last Modified: 2010-03-30
This is somewhat a general programming question but how can I go to a web site such as the Navy's atomic clock time page, or any other source, and get the official time of day without really viewing the page?

In other words, I want to write a program that can go to this website get the time and store it in a buffer. I don't know exactly how to retrieve the time. (I know where the pages are but the time is displayed via some cgi program on there server).
0
Comment
Question by:rutledgj
  • 2
4 Comments
 
LVL 4

Expert Comment

by:seguret
ID: 1222677
What's the page ?

0
 

Author Comment

by:rutledgj
ID: 1222678
0
 
LVL 4

Expert Comment

by:seguret
ID: 1222679
It doesn't seem to be hard to write a program that connect to the cgi and read the image.
I never tried to read x-bitmap files but it doesn't seem to be hard to recognize digits.

But it's one hour of code writting and I can hardly imagine an usage of your application, time isn't precise and it's not so difficult to obtain time in a java code.
0
 
LVL 7

Accepted Solution

by:
yoren earned 100 total points
ID: 1222680
How much accuracy do you need, and what exactly are you trying to accomplish?

You're not going to be able to get the time from the US Naval Observatory Web page, because they display the time as a GIF. Otherwise, you'd be able to parse the HTML to get the page.

If you're just looking to set your local clock to the official time, check out:


Good luck,

Yuval
http://www.kcmultimedia.com/jsntp/

Otherwise, if being off by a few seconds is acceptable, I would write a little CGI that does nothing but return the time:

#!/bin/sh
echo "Content-type: text/plain"
echo ""
date -u

Then, have your Java program call this CGI, fetch the time, and parse it. Something like this (but with error handling)

URL url = new URL("http://myserver/time.cgi");
URLConnection urlcon = url.openConnection();

// We want to get data from the URL
urlcon.setDoInput(true);

// Open the stream of the CGI's output
DataInputStream is = new DataInputStream(urlcon.getInputStream());

// Read the date
Byte[] bytesDate = new byte[30];
is.readFully(bytesDate);
Date dateOfficial = DateFormat.parse(new String(bytesDate));

The only caveat is that you'll be a few seconds off. To be precise, you'll be off the amount of time between your CGI's system call to "date" and when your Java program has finished receiving the date string and has parsed it.

If you need accuracy down to several milliseconds, you'll probably have to implement the SNTP protocol yourself. It's kind of complicated, but you can check out this URL for the protocol spec:

http://www.alternic.net/info/rfcs/1300/rfc1361.txt.html
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now