[Android] Daytime query (11.12 Update)

Socket mServer = new Socket(DAY_TIME_SERVER, 13);

InputStream timeInput = mServer.getInputStream();
StringBuffer timeStamp = new StringBuffer();

int ch;
while ((ch = timeInput.read()) != -1)
timeStamp.append((char) ch);

String time = timeStamp.toString().trim();
System.out.println("Time: + "@" + DAY_TIME_SERVER);

zzZ...

Daytime Protocol

Using SimpleDateFormat to convert the String to Date.

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Date d = df.parse("2011-11-12 20:16:58");
Calendar c = Calendar.getInstance();
c.setTime(d);
c.add(Calendar.HOUR, -8);
d = c.getTime();


Reference:
SimpleDateFormat Tutorial
Calendar Tutorial

留言

這個網誌中的熱門文章

逃得了一時 逃不了一世

Google Hangouts

我老闆是真男人!