[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

java and downloading time

Posted on 1998-08-27
4
Medium Priority
?
179 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

650 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