62,620
社区成员
发帖
与我相关
我的任务
分享
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
public class TimeControl {
public static void main(String[] args) {
if(args.length != 2){
System.out.println("用法:");
System.out.println("第一个参数: 时间增量");
System.out.println("第二个参数: 时间单位 - S (秒), M (分), H(时)");
return;
}
// TODO Auto-generated method stub
LocalDate startDate = LocalDate.of(2015, Month.OCTOBER, 23);
LocalTime startTime = LocalTime.of(11, 40, 00);
LocalDateTime start = LocalDateTime.of(startDate, startTime);
LocalDate endDate = LocalDate.of(2015, Month.OCTOBER, 25);
LocalTime endTime = LocalTime.of(11, 40, 00);
LocalDateTime end = LocalDateTime.of(endDate, endTime);
String unit = args[1].toUpperCase();
int amount = 0;
try{
amount = Integer.parseInt(args[0]);
}catch(NumberFormatException nfe){
System.out.println("invalid number, whole number only");
return;
}
TemporalUnit tUnit = null;
switch(unit){
case "S":
tUnit = ChronoUnit.SECONDS;
break;
case "M":
tUnit = ChronoUnit.MINUTES;
break;
case "H":
tUnit = ChronoUnit.HOURS;
break;
default:
tUnit = ChronoUnit.MINUTES;
}
Duration d = Duration.of(amount, tUnit);
while (start.isBefore(end)) {
System.out.println(start.toLocalDate() + " " + start.toLocalTime().format(DateTimeFormatter.ISO_LOCAL_TIME));
start = start.plus(d);
}
}
}