?
Solved

java and downloading time

Posted on 1998-08-27
4
Medium Priority
?
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

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

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 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 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