我在JS中有以下功能:
var startDate = stringToDate('12/01/2016','mm/dd/yyyy','/');
var endDate = stringToDate('01/01/2017','mm/dd/yyyy','/');
function compareDate(_date) {
var val1 = ( startDate <= _date)
var val2 = ( endDate >= _date )
var val = val1 && val2
log('--------')
log(_date)
log(startDate)
log(endDate)
log(val1)
log(val2)
log(val)
log('--------')
return val
}
日志
[17-02-10 13:25:14:145 EET] 2016年12月2日星期五00:00:00 GMT + 0200(EET)
[17-02-10 13:25:14:146 EET] Thu Dec 01 00:00:00 GMT + 02:00 2016
[17-02-10 13:25:14:147 EET] Sun Jan 01 00:00:00 GMT + 02:00 2017
[17-02-10 13:25:14:147 EET] false
[17-02-10 13:25:14:148 EET] false
[17-02-10 13:25:14:148 EET] false
[17-02-10 13:25:14:149 EET] --------
[17-02-10 13:25:14:149 EET] --------
[17-02-10 13:25:14:150 EET] 2016年12月3日星期六00:00:00 GMT + 0200(EET)
[17-02-10 13:25:14:150 EET] Thu Dec 01 00:00:00 GMT + 02:00 2016
[17-02-10 13:25:14:151 EET] Sun Jan 01 00:00:00 GMT + 02:00 2017
[17-02-10 13:25:14:151 EET] false
[17-02-10 13:25:14:152 EET] false
[17-02-10 13:25:14:152 EET] false
[17-02-10 13:25:14:152 EET] --------
[17-02-10 13:25:14:153 EET] --------
[17-02-10 13:25:14:153 EET] Sun Dec 04 2016 00:00:00 GMT + 0200(EET)
[17-02-10 13:25:14:154 EET] Thu Dec 01 00:00:00 GMT + 02:00 2016
[17-02-10 13:25:14:154 EET] Sun Jan 01 00:00:00 GMT + 02:00 2017
[17-02-10 13:25:14:155 EET] false
[17-02-10 13:25:14:155 EET] false
[17-02-10 13:25:14:156 EET] false
正如您在第一个日志Dec 02
中看到的那样,Dec 01
大于public class MainActivity extends AppCompatActivity {
TextView infoIp, infoPort;
static final int SocketServerPORT = 8080;
ServerSocket serverSocket;
ServerSocketThread serverSocketThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infoIp = (TextView) findViewById(R.id.infoip);
infoPort = (TextView) findViewById(R.id.infoport);
infoIp.setText(getIpAddress());
serverSocketThread = new ServerSocketThread();
serverSocketThread.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + "\n";
}
}
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
public class ServerSocketThread extends Thread {
@Override
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SocketServerPORT);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
infoPort.setText("I'm waiting here: "
+ serverSocket.getLocalPort());
}});
while (true) {
socket = serverSocket.accept();
FileTxThread fileTxThread = new FileTxThread(socket);
fileTxThread.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public class FileTxThread extends Thread {
Socket socket;
FileTxThread(Socket socket){
this.socket= socket;
}
@Override
public void run() {
File file = new File(
Environment.getExternalStorageDirectory(),
"graphs.txt");
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bis.read(bytes, 0, bytes.length);
OutputStream os = socket.getOutputStream();
os.write(bytes, 0, bytes.length);
os.flush();
socket.close();
final String sentMsg = "File sent to: " + socket.getInetAddress();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,
sentMsg,
Toast.LENGTH_LONG).show();
}});
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
,但我得到了假等等......
答案 0 :(得分:0)
您的问题中缺少一些代码,但我添加了我的版本 - &gt;将字符串日期更改为格式为yyyymmdd
的整数,并比较>=
&amp;的整数<=
(因为字符串比较可能是错误的):
function stringToDateInt(_date, _format, _sep)
{
var aDate = _date .split(_sep);
var aForm = _format.split(_sep);
//console.log(aDate);
//console.log(aForm);
var oDate = {};
for(var i = 0, length = aDate.length; i < length; i++)
{
oDate[aForm[i]] = aDate[i];
}
//console.log(oDate);
return (oDate.yyyy + oDate.mm + oDate.dd) * 1;
}
var startDate = stringToDateInt('12/01/2016','mm/dd/yyyy','/');
var endDate = stringToDateInt('01/01/2017','mm/dd/yyyy','/');
function compareDate(_date)
{
var iDate = stringToDateInt(_date,'mm/dd/yyyy','/');
var val1 = ( startDate <= iDate);
var val2 = ( endDate >= iDate );
var val = val1 && val2;
console.log('--------')
console.log(_date, '->', iDate);
console.log(startDate)
console.log(endDate)
console.log('Date Bigger than or equal startDate:', val1)
console.log('Date Less than or equal endDate:', val2)
console.log('Date between start and end dates:', val)
console.log('--------')
return val
}
compareDate('12/02/2016');
compareDate('01/01/2015');
compareDate('11/11/2017');
或者日期版本更接近你正在做的事情:
function stringToDate(_date, _format, _sep)
{
var aDate = _date .split(_sep);
var aForm = _format.split(_sep);
//console.log(aDate);
//console.log(aForm);
var oDate = {};
for(var i = 0, length = aDate.length; i < length; i++)
{
oDate[aForm[i]] = aDate[i];
}
//console.log(oDate);
//return (oDate.yyyy + oDate.mm + oDate.dd) * 1;
return new Date(oDate.yyyy, (oDate.mm * 1 - 1), oDate.dd);
}
var startDate = stringToDate('12/01/2016','mm/dd/yyyy','/');
var endDate = stringToDate('01/01/2017','mm/dd/yyyy','/');
function compareDate(_date)
{
var val1 = ( startDate <= _date);
var val2 = ( endDate >= _date );
var val = val1 && val2;
console.log('--------')
console.log(_date, '->', _date);
console.log(startDate)
console.log(endDate)
console.log('Date Bigger than or equal startDate:', val1)
console.log('Date Less than or equal endDate:', val2)
console.log('Date between start and end dates:', val)
console.log('--------')
return val
}
compareDate(stringToDate('12/02/2016','mm/dd/yyyy','/'));
compareDate(stringToDate('01/01/2015','mm/dd/yyyy','/'));
compareDate(stringToDate('11/11/2017','mm/dd/yyyy','/'));