我对这里的逻辑有点困难,现在已经很晚了,说实话,我很难过。
我需要遍历时隙。
public class Junk {
private static final int BUFFER_SIZE = 127;
private static final String CHARSET = "UTF-8";
public static void main(String[] args) {
try {
String fileName = "two.txt";
myWay(fileName);
otherWay(fileName);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private static void myWay(String fileName) throws IOException {
System.out.println("I did it MY WAY!......");
FileChannel channel = FileChannel.open(Paths.get(fileName), StandardOpenOption.READ);
// I tried both `allocate` and `allocateDirect`
ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
int bytesRead = channel.read(buffer);
channel.close();
// Manually build the string from the ByteBuffer.
// This is ONLY to validate the buffer has the content
StringBuilder sb = new StringBuilder();
for(int i=0;i<bytesRead;i++){
sb.append((char)buffer.get(i));
}
System.out.println("manual string='"+sb+"'");
CharBuffer charBuffer = Charset.forName(CHARSET).decode(buffer);
// WHY FOR YOU NO HAVE THE CHARS??!!
System.out.println("CharBuffer='" + new String(charBuffer.array()) + "'");
System.out.println("CharBuffer='" + charBuffer.toString() + "'");
System.out.println("........My way sucks.");
}
private static void otherWay(String fileName) throws IOException{
System.out.println("The other way...");
FileChannel channel = FileChannel.open(Paths.get(fileName), StandardOpenOption.READ);
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
channel.close();
Charset chars = Charset.forName(CHARSET);
CharBuffer cbuf = chars.decode(buffer);
String str = new String(cbuf.array());
System.out.println("str = '" + str + "'");
System.out.println("...works.");
}
}
所以我可以指定一个时隙间隙,所以如果我要遍历时隙(间隔30分钟),那就是:
var settings = {
startOfWeek:0, //0 = Sunday, 1 = Monday
timeSlotGap: 30,
minTime: "09:00:00",
maxTime: "17:30:00",
numSlots: 0
};
目前我有以下内容:
09:00
09:30
10:00
10:30
如果简单到循环30/60分钟就不会有问题,但由于我可以为时间段指定任何内容,即90分钟,这使得它有点困难。
答案 0 :(得分:3)
要获得时间段,您可以使用以下内容:
var settings = {
startOfWeek:0, //0 = Sunday, 1 = Monday
timeSlotGap: 30,
minTime: "09:00:00",
maxTime: "17:30:00",
numSlots: 0
};
function getTimeDate(time) {
var timeParts = time.split(':');
var d = new Date();
d.setHours(timeParts[0]);
d.setMinutes(timeParts[1]);
d.setSeconds(timeParts[2]);
return d;
}
function getTimeSlots(startDate, endDate, interval) {
var slots = [];
var intervalMillis = interval * 60 * 1000;
while (startDate < endDate) {
// So that you get "00" if we're on the hour.
var mins = (startDate.getMinutes() + '0').slice(0, 2);
slots.push(startDate.getHours() + ':' + mins);
startDate.setTime(startDate.getTime() + intervalMillis);
}
return slots;
}
var slots = getTimeSlots(
getTimeDate(settings.minTime), getTimeDate(settings.maxTime), settings.timeSlotGap
);
以下是JSFiddle的示例。