java 我需要排除星期六和星期日从这个程序显示的日期,每15天

jdgnovmf  于 2023-05-27  发布在  Java
关注(0)|答案(1)|浏览(121)

我正在尝试做一个程序,每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。

s4n0splo

s4n0splo1#

tl;dr

package work.basil.example.time;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.EnumSet;
import java.util.Set;

public class FifteenDays
{
    public static void main ( String[] args )
    {
        ZoneId z = ZoneId.of ( "Africa/Tunis" );
        LocalDate today = LocalDate.now ( z );

        int daysToJump = 15 , jumps = 5;  // Jump to 0, 15, 30, 45, 60 days out.
        Set < DayOfWeek > weekend = EnumSet.of ( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY );
        LocalDate localDate = today;
        for ( int jumpIndex = 0 ; jumpIndex < jumps ; jumpIndex++ )
        {
            int daysJumped = ( daysToJump * jumpIndex );
            localDate = localDate.plusDays ( daysJumped );
            if ( weekend.contains ( localDate.getDayOfWeek ( ) ) ) continue;
            System.out.println ( "jumpIndex: " + jumpIndex + " | daysJumped: " + daysJumped + " | localDate = " + localDate );
        }
    }
}

详情

LocalDate today =
要捕获当前日期,您需要一个时区。如果省略,则会隐式应用JVM的当前默认时区。要明白,对于任何给定的时刻,👉日期在地球仪各地因时区而异;* 明天在东京 * 日本是同时 * 昨天在托莱多 * 俄亥俄州。

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate today = LocalDate.now( z ) ;

显示每15天的日期
定义要跳转的天数以及要跳转的次数。

int daysToJump = 15 , jumps = 5;  // Jump to 0, 15, 30, 45, 60 days out.

定义我们将跳跃的第一天。

LocalDate localDate = today;

循环我们想要的五次迭代,添加0,15,30,45,60。在每个上添加daysToJump

for ( int jumpIndex = 0 ; jumpIndex < jumps ; jumpIndex++ ) { … }
for ( int jumpIndex = 0 ; jumpIndex < jumps ; jumpIndex++ )
{
    int daysJumped = ( daysToJump * jumpIndex );
    localDate = localDate.plusDays ( daysJumped );
    System.out.println ( "jumpIndex: " + jumpIndex + " | daysToJump: " + daysToJump + " | localDate = " + localDate );
}

运行时。

jumpIndex: 0 | daysJumped: 0 | localDate = 2023-05-25
jumpIndex: 1 | daysJumped: 15 | localDate = 2023-06-09
jumpIndex: 2 | daysJumped: 30 | localDate = 2023-07-09
jumpIndex: 3 | daysJumped: 45 | localDate = 2023-08-23
jumpIndex: 4 | daysJumped: 60 | localDate = 2023-10-22

你说:
我需要去掉周六和周日
是的,我们可以跳过那些在周末的约会。
将周末定义为一周中的set天。Java提供了七个预定义的enum对象,每个day of the week一个,周一到周日。

Set < DayOfWeek > weekend = EnumSet.of ( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY );

对于每次跳转,添加要跳转的日期,看看该日期是否在周末。如果是周末,忽略这个日期。如果是工作日,打印。在循环中添加一个星期几的测试。

for ( int jumpIndex = 0 ; jumpIndex < jumps ; jumpIndex++ )
{
    int daysJumped = ( daysToJump * jumpIndex );
    localDate = localDate.plusDays ( daysJumped );
    if ( weekend.contains ( localDate.getDayOfWeek ( ) ) ) continue;
    System.out.println ( "jumpIndex: " + jumpIndex + " | daysJumped: " + daysJumped + " | localDate = " + localDate );
}

运行时:

jumpIndex: 0 | daysJumped: 0 | localDate = 2023-05-25
jumpIndex: 1 | daysJumped: 15 | localDate = 2023-06-09
jumpIndex: 3 | daysJumped: 45 | localDate = 2023-08-23

相关问题