简体   繁体   English

如何将Date对象转换为字符串/ long并通过客户端/服务器发送

[英]how to convert Date object to string/long and send it via client/server

Hi i'm having a little trouble with sending a Date object from a Client to a Server so that i can compare them. 嗨,我在将Date对象从客户端发送到服务器时遇到了一些麻烦,以便可以对其进行比较。 So basically what i'm trying to do is: 1)Send the Date object 2)Compare their times 3)Set the time to both machines with the average time from both 所以基本上我想做的是:1)发送Date对象2)比较它们的时间3)设置两台计算机的时间,同时计算两者的平均时间

So far i have done this: 到目前为止,我已经做到了:

Server: 服务器:

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());
}
}

Client: 客户:

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();
        }


    }
}

But the result i get is 'nothing' just white space. 但是我得到的结果仅仅是空白。 The program compiles fine with now errors and i kind of know where the problem is but i just can't figure out how to parse the values correctly so that they can be read properly. 该程序现在可以正确编译,并带有错误,并且我知道问题出在哪里,但我只是想不出如何正确解析值,以便可以正确读取它们。 Please help me :S 请帮我:S

In your client you send the date as a string formated by SimpleDateFormat resulting in something like: "Sat Mai 04 22:27:24 PST 2013" 在您的客户端中,将日期作为由SimpleDateFormat格式化的字符串发送,结果类似:“ Sat Mai 04 22:27:24 PST 2013”

On the server side you take this string an tries to parse it as a long so that you can use it future in the Date(long date) constructor. 在服务器端,您可以使用此字符串尝试将其解析为long,以便将来可以在Date(long date)构造函数中使用它。

The new Date(long date) expect date to be unixtime. 新的Date(长日期)期望日期为unixtime。 Unixtime is what you get when you call date.getTime(). 调用date.getTime()会得到Unixtime。 Unixtime is milliseconds sins 01.01.1970 00:00:00:0000. Unixtime是毫秒为单位的毫秒,即01.01.1970 00:00:00:0000。

I would expect server to throw som kind of parse exception. 我希望服务器抛出som类的解析异常。

You have to deside if you want to use SimpleDateFormat to format transmittion side and the use SimpleDateFormat parse on the reciving side. 如果要使用SimpleDateFormat格式化发送端,而要在接收端使用SimpleDateFormat解析,则必须离开。 Or send the unixtime. 或发送unixtime。

I would've recommend sending the unixtime. 我建议发送unixtime。 You convert from Date object by: 您可以通过以下方式从Date对象转换:

Long unixtime =  date.getTime() 

And back: 然后回来:

Date date = new Date(unixtime)

or serverside you dont need the date object at all, you only use it to call getTime() witch in turn gives back unixtime. 或在服务器端根本不需要日期对象,只需使用它来调用getTime()即可,然后又返回unixtime。

Server: 服务器:

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());
}
}

Client: 客户:

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();
        }


    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM