Solved

java and downloading time

Posted on 1998-08-27
4
174 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

792 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