Solved

java and downloading time

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 107
Java Timer (static) 9 45
ForLoop Example 3 49
String array comparison 4 34
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

821 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