我正在尝试做一个程序,每15天显示一次日期。
import java.time.*;
import java.util.Scanner;
;
public class test{
public static final String ANSI_RESET = "\u001B[0m"; //ANSI Color code //only for colored text.
public static final String ANSI_YELLOW = "\u001B[33m"; //ANSI Color code //only for colored text.
public static final String ANSI_RED = "\u001B[31m"; //ANSI Color code //only for colored text.
public static final String ANSI_GREEN = "\u001B[32m"; //ANSI Color code //only for colored text.
public static final String ANSI_BLUE = "\u001B[34m"; //ANSI Color code //Only for colored text.
public static final String ANSI_PURPLE = "\u001B[35m"; //ANSI Color code //only for colored text.
public static final String ANSI_CYAN = "\u001B[36m"; //ANSI Color code //only for colored text.
public static final String ANSI_WHITE = "\u001B[37m"; //ANSI Color code //only for colored text.
public static void main(String[]args) {
System.out.println(ANSI_YELLOW + "Bi-Weekly Calculator" + ANSI_RESET); //Introduction
try {
Thread.sleep(2000); //Pause for 2 seconds
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(ANSI_YELLOW + "This calculator will only show dates for the next 12 months." + ANSI_RESET);
try {
Thread.sleep(2000);//Pause for 2 seconds
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Scanner scanner = new Scanner(System.in); // Date input from user
System.out.print(ANSI_RED + "Enter Date : " + ANSI_RESET);
String INPUT_DATE = scanner.next();
try {
Thread.sleep(2000);
} catch (InterruptedException e) { //Line 37 to 41 for pausing the code for 2 seconds.
throw new RuntimeException(e);
}
LocalDate today = LocalDate.parse(INPUT_DATE);
System.out.println("\n"+today);
System.out.println();
System.out.println(today.plusDays(15)+"\n"); //days added to input date.
System.out.println(today.plusDays(30)+"\n");
System.out.println(today.plusDays(45)+"\n");
System.out.println(today.plusDays(60)+"\n");
}
}
程序接受用户的输入,然后加上15,然后是30,然后是45,依此类推。我需要o删除周六和周日从这个程序,因为我实际上使这个程序的每15天支付的人。
我在google上没有找到任何好的解决方案,而且我对java有点陌生。我使用JDK 20.0.1和IntelliJ IDEA作为IDE。
1条答案
按热度按时间s4n0splo1#
tl;dr
详情
LocalDate today =
要捕获当前日期,您需要一个时区。如果省略,则会隐式应用JVM的当前默认时区。要明白,对于任何给定的时刻,👉日期在地球仪各地因时区而异;* 明天在东京 * 日本是同时 * 昨天在托莱多 * 俄亥俄州。
显示每15天的日期
定义要跳转的天数以及要跳转的次数。
定义我们将跳跃的第一天。
循环我们想要的五次迭代,添加0,15,30,45,60。在每个上添加
daysToJump
。运行时。
你说:
我需要去掉周六和周日
是的,我们可以跳过那些在周末的约会。
将周末定义为一周中的set天。Java提供了七个预定义的enum对象,每个day of the week一个,周一到周日。
对于每次跳转,添加要跳转的日期,看看该日期是否在周末。如果是周末,忽略这个日期。如果是工作日,打印。在循环中添加一个星期几的测试。
运行时: