我正在尝试检查当前时间(小时和分钟)是否在特定时间之后或之前,如何在Kotlin中进行操作
答案 0 :(得分:1)
您可以使用Calendar
类:
import com.ericsson.otp.erlang.*;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class Main {
public static OtpErlangList StringListToErlangList(List<String> strs) {
OtpErlangObject[] elems = new OtpErlangObject[strs.size()];
int idx = 0;
for (String str : strs) {
elems[idx] = new OtpErlangString(str);
idx++;
}
return new OtpErlangList(elems);
}
public static List<String> ErlangListToStringList(OtpErlangList estrs) {
OtpErlangObject[] erlObjs = estrs.elements();
List<String> strs = new LinkedList<String>();
for (OtpErlangObject erlO : erlObjs) {
strs.add(erlO.toString());
}
return strs;
}
public static void main(String[] args) throws IOException, InterruptedException {
// Do some initial setup.
OtpNode node = new OtpNode("alice", "secret");
OtpMbox mbox = node.createMbox();
mbox.registerName("alice");
// Check that the remote node is actually online.
if (node.ping("bob@127.0.0.1", 2000)) {
System.out.println("remote is up");
} else {
System.out.println("remote is not up");
}
// Create the list of strings that needs to be sent to the other node.
List<String> strs = new LinkedList<String>();
strs.add("foo");
strs.add("bar");
OtpErlangList erlangStrs = StringListToErlangList(strs);
// Create a tuple so the other node can reply to use.
OtpErlangObject[] msg = new OtpErlangObject[2];
msg[0] = mbox.self();
msg[1] = erlangStrs;
OtpErlangTuple tuple = new OtpErlangTuple(msg);
// Send the tuple to the other node.
mbox.send("echo", "bob@127.0.0.1", tuple);
// Await the reply.
while (true) {
try {
System.out.println("Waiting for response!");
OtpErlangObject o = mbox.receive();
if (o instanceof OtpErlangList) {
OtpErlangList erlList = (OtpErlangList) o;
List<String> receivedStrings = ErlangListToStringList(erlList);
for (String s : receivedStrings) {
System.out.println(s);
}
}
if (o instanceof OtpErlangTuple) {
OtpErlangTuple m = (OtpErlangTuple) o;
OtpErlangPid from = (OtpErlangPid) (m.elementAt(0));
OtpErlangList value = (OtpErlangList) m.elementAt(1);
List<String> receivedStrings = ErlangListToStringList(value);
for (String s : receivedStrings) {
System.out.println(s);
}
}
} catch (OtpErlangExit otpErlangExit) {
otpErlangExit.printStackTrace();
} catch (OtpErlangDecodeException e) {
e.printStackTrace();
}
}
}
}
答案 1 :(得分:1)
如果您有LocalDateTime
或类似名称,则可以提取MINUTE_OF_DAY
(基本上是小时+分钟)来比较时间,而忽略其余时间,例如:
val now = LocalDateTime.now()
val dateToCompare : LocalDateTime = TODO()
val minutesOfDayNow = now.get(ChronoField.MINUTE_OF_DAY)
val minutesOfDayToCompare = dateToCompare.get(ChronoField.MINUTE_OF_DAY)
when {
minutesOfDayNow == minutesOfDayToCompare -> // same hour and minute of day
minutesOfDayNow > minutesOfDayToCompare -> // hours and minutes now are after the time to compare (only in regards to hours and minutes... not day/month/year or whatever)
minutesOfDayNow < minutesOfDayToCompare -> // hours and minutes now are before the time to compare... same conditions apply
}
如果您有Date
,则可能有兴趣将其转换为java.time
类型的实例,例如:
fun Date.minutesOfDay() = toInstant().atZone(ZoneId.systemDefault()).get(ChronoField.MINUTE_OF_DAY)
val now = Date()
val dateToCompare : Date = TODO()
if (now.minutesOfDay() > dateToCompare.minutesOfDay()) // ...etc. pp.
最后,如果您想使用Calendar
,请确保只比较您感兴趣的事物,例如小时和分钟,仅此而已,例如:
val now = Calendar.getInstance()
val nowInMinutes = now[Calendar.HOUR_OF_DAY] * 60 + now[Calendar.MINUTE]
val dateInMinutesToCompare = hours * 60 + minutes
when {
nowInMinutes == dateInMinutesToCompare -> // equal
nowInMinutes > dateInMinutesToCompare -> // now after given time
else -> // now before given time
}