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:


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.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 = "";

        Socket s = new Socket("", HTTP_PORT);
        System.out.println("Connected to Getting return string..");
        InputStream instream = s.getInputStream();
        Scanner in = new Scanner(instream);
        inStr = in.toString();
        System.out.println("Return string retrieved: " + inStr);

        catch(UnknownHostException e){
        catch(IOException i){

        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");
        String dateStr = currDate.toString();
        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("", HTTP_PORT);
 System.out.println("Connected to Getting return string..");
 BufferedReader br = 
      new BufferedReader( new InputStreamReader( s.getInputStream() ) );
 for( String temp = br.readLine(); temp != null; temp = br.readLine() )
   inStr += temp.trim();
 System.out.println("Return string retrieved: " + inStr);
This article has been dead for over six months. Start a new discussion instead.