For my Programming class I have to write a program to get the Date and time from a Daytime protocol server. If you don't know, the format for the Daytime protocol is as follows:

JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) *

YR being the last two digits of the year, MO being month, DA being day, HH MM and SS being hours minutes and seconds respectively.

I've to make a program to use a socket to get a return string in that format from a server, return that string as well as a string formatted by Date(), as well as a string formatted using SimpleDateFormat.

The problem I have right now is I'm getting an IOException (where I have caught the IOException), and I'm getting a StringIndexOutOfBoundsException at line 31 where I start to use substring, particularly on the "7" apparently.

Here's my code, hopefully you can help. Thanks!

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.text.*;


public class timeServerGet {
    public static void main(String args[]){
        final int HTTP_PORT = 13;
        String inStr = "";
        String formStr = "";

        try{
        Socket s = new Socket("tima-a.nist.gov", HTTP_PORT);
        System.out.println("Connected to time-a.nist.gov. Getting return string..");
        InputStream instream = s.getInputStream();
        Scanner in = new Scanner(instream);
        inStr = in.toString();
        System.out.println("Return string retrieved: " + inStr);
        instream.close();

        }
        catch(UnknownHostException e){
            System.out.println("UnknownHostException");
        }
        catch(IOException i){
            System.out.println("IOException");
        }

        String year = inStr.substring(6,7);
        String month = inStr.substring(9,10);
        String day = inStr.substring(12,13);
        String hour = inStr.substring(15,16);
        String min = inStr.substring(18,19);
        String sec = inStr.substring(21,22);

        int yy = Integer.parseInt(year);
        int mm = Integer.parseInt(month);
        int dd = Integer.parseInt(day);
        int hh = Integer.parseInt(hour);
        int mn = Integer.parseInt(min);
        int ss = Integer.parseInt(sec);

        Date currDate = new Date(yy,mm,dd,hh,mn,ss);
        SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy G 'at' HH:MM:ss Z");
        sdf.setTimeZone(TimeZone.getTimeZone("EST"));
        String dateStr = currDate.toString();
        try{
        Date formDate = sdf.parse(dateStr);
        formStr = formDate.toString();
        }
        catch(ParseException x){
            System.out.println("ParseException at formDate");
        }

        System.out.println("Return string:" + inStr);
        System.out.println("Date no format:" + dateStr);
        System.out.println("Date with format:" + formStr);
        






    }

}

You had the hostname spelling wrong for one thing and your use of Scanner doesn't look right. You program still isn't quite right though. Remember, it's possible to telnet into servers that use "text" format which can help a lot in trouble shooting.

Socket s = new Socket("time-a.nist.gov", HTTP_PORT);
 System.out.println("Connected to time-a.nist.gov. Getting return string..");
 BufferedReader br = 
      new BufferedReader( new InputStreamReader( s.getInputStream() ) );
        
 for( String temp = br.readLine(); temp != null; temp = br.readLine() )
 {
   inStr += temp.trim();
 }
       
 s.close();
 System.out.println("Return string retrieved: " + inStr);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.