大神进~在线等~

qq_14893917 2017-01-17 01:42:36
需求:希望控制台输出以下数据
2015-10-23 11:40:21
2015-10-24 11:43:00
2015-10-25 11:45:32
范围:2015-10-23 11:40:00 ~2015-10-25 11:40:00
时间递增,时分秒控制在我给定的范围
...全文
362 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_14893917 2017-01-17
  • 打赏
  • 举报
回复
qq_14893917 2017-01-17
  • 打赏
  • 举报
回复
引用 3 楼 soton_dolphin 的回复:

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);
		}
	}

}
运行不起来。。
soton_dolphin 2017-01-17
  • 打赏
  • 举报
回复

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);
		}
	}

}
斯卡洛特 2017-01-17
  • 打赏
  • 举报
回复
思路:按范围随机3个数,数字要比之前的大
xiesisi3 2017-01-17
  • 打赏
  • 举报
回复
作业要自己做才能学到东西

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧