0

嗨,我在将 Date 对象从客户端发送到服务器时遇到了一点麻烦,以便我可以比较它们。所以基本上我要做的是:1)发送日期对象2)比较他们的时间3)将时间设置为两台机器的平均时间

到目前为止,我已经这样做了:

服务器:

public class Server implements Runnable{
    private int port;
    private String name;
    private Date mydate = new Date();
    private Date date;
    public Server(int port, String name){
        this.port=port;
        this.name=name;
    }
    public synchronized void run(){
        while(true){
        try{

            method();

        }
        catch(Exception e){
            return;
        }

        }
    }

    public synchronized void method() throws Exception{
        long dates;
        long average=0;
        String[] values = new String[10];

        DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
      ServerSocket server = new ServerSocket(port);

      Socket s=server.accept();

      InputStream in= s.getInputStream();
      OutputStream out = s.getOutputStream();

      PrintWriter w = new PrintWriter(out);
      Scanner r = new Scanner(in);
      for(int i=0; i<10; i++){
      String msg = r.next();
      values[i] = msg;

    }
      for(int j=0; j<values.length; j++){
          dates = Long.parseLong(values[j]);
          date = new Date(dates);
          average = average + date.getTime();
      }
      average = (average - mydate.getTime())/2;
      mydate.setTime(average);
      System.out.println(name+": "+mydate.toString());
}
}

客户:

public class Client implements Runnable{
    private int port;
    private int id;
    Date time = new Date();
    Date time2 = new Date();
    public Client(int port,int id){
        this.port=port;
        this.id = id;
    }

    public synchronized void run(){

        try{
            Thread.sleep(5000);

            method();

        }
        catch(Exception e){
            return;
        }

    }

    public synchronized void method() throws Exception{
        DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
        Random ran = new Random(500);
        int d = ran.nextInt(500)+500;
        time.setTime(time.getTime()+d);
        String datestr;
        Socket s = new Socket("localhost", port);

        InputStream in= s.getInputStream();
        OutputStream out = s.getOutputStream();

        PrintWriter w = new PrintWriter(out);
        Scanner r = new Scanner(in);
        for(int i=0; i<10; i++){
        time.setTime(time.getTime()+d);
        datestr = df.format(time);
        w.println(datestr);
        w.flush();
        }


    }
}

但我得到的结果是“没有”只是空白。该程序编译得很好,现在出现错误,我有点知道问题出在哪里,但我只是不知道如何正确解析这些值,以便正确读取它们。请帮助我:S

4

1 回答 1

0

在您的客户端中,您将日期作为由 SimpleDateFormat 格式化的字符串发送,结果类似于:“Sat Mai 04 22:27:24 PST 2013”

在服务器端,您将此字符串尝试将其解析为 long,以便您可以在 Date(long date) 构造函数中使用它。

新日期(长日期)预计日期为 unixtime。Unixtime 是调用 date.getTime() 时得到的。Unixtime 是毫秒 sins 01.01.1970 00:00:00:0000。

我希望服务器会抛出某种解析异常。

您必须决定是否要使用 SimpleDateFormat 来格式化传输端并在接收端使用 SimpleDateFormat 解析。或者发送unixtime。

我会建议发送 unixtime。您可以通过以下方式从 Date 对象转换:

Long unixtime =  date.getTime() 

然后回来:

Date date = new Date(unixtime)

或服务器端你根本不需要日期对象,你只用它来调用 getTime() 女巫反过来返回 unixtime。

服务器:

public class Server implements Runnable{
    private int port;
    private String name;
    private Date mydate = new Date();
    private Date date;
    public Server(int port, String name){
        this.port=port;
        this.name=name;
    }
    public synchronized void run(){
        while(true){
        try{

            method();

        }
        catch(Exception e){
            return;
        }

        }
    }

    public synchronized void method() throws Exception{
        long unixtime;
        long average=0;
        String[] values = new String[10];

        ServerSocket server = new ServerSocket(port);

      Socket s=server.accept();

      InputStream in= s.getInputStream();
      OutputStream out = s.getOutputStream();

      PrintWriter w = new PrintWriter(out);
      Scanner r = new Scanner(in);
      for(int i=0; i<10; i++){
      String msg = r.next();
      values[i] = msg;

    }
      for(int j=0; j<values.length; j++){
          unixtime= Long.parseLong(values[j]);
          average = average + unixtime;
      }
      average = (average - mydate.getTime())/2;
      mydate.setTime(average);
      System.out.println(name+": "+mydate.toString());
}
}

客户:

public class Client implements Runnable{
    private int port;
    private int id;
    Date time = new Date();
    Date time2 = new Date();
    public Client(int port,int id){
        this.port=port;
        this.id = id;
    }

    public synchronized void run(){

        try{
            Thread.sleep(5000);

            method();

        }
        catch(Exception e){
            return;
        }

    }

    public synchronized void method() throws Exception{
        Random ran = new Random(500);
        int d = ran.nextInt(500)+500;
        time.setTime(time.getTime()+d);
        Socket s = new Socket("localhost", port);

        InputStream in= s.getInputStream();
        OutputStream out = s.getOutputStream();

        PrintWriter w = new PrintWriter(out);
        Scanner r = new Scanner(in);
        for(int i=0; i<10; i++){
        time.setTime(time.getTime()+d);
        w.println(time.getTime());
        w.flush();
        }


    }
}
于 2013-05-04T15:22:31.217 回答