英文:
I need to exclude Saturdays and Sundays from this program that shows dates of every 15 days
问题
I can help you with the translation. Here is the translated text:
我正在尝试创建一个程序,显示每隔15天的日期。
该程序接受用户输入,并分别添加15天、30天、45天等。我需要从该程序中排除星期六和星期日,因为我实际上是为某人的每15天工资制作这个程序。
我在Google上没有找到合适的解决方案,而且我在Java方面相对新手。我正在使用JDK 20.0.1和IntelliJ IDEA作为IDE。
英文:
I'm trying to make a program that shows dates of every 15 days.
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");
}
}
The program takes input from the user and adds 15, then 30, then 45, and so on. I need o remove Saturdays and Sundays from this program as I'm actually making this program for the every 15 day pay for someone.
I didn't find any good solution on google, and I'm kinda new with java. I'm using JDK 20.0.1 and IntelliJ IDEA as IDE.
答案1
得分: 1
以下是代码的翻译部分:
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; // 跳转到0、15、30、45、60天后。
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 );
}
}
}
希望这对您有所帮助。如果您需要更多的翻译,请告诉我。
英文:
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 );
}
}
}
Details
> LocalDate today =
To capture the current date, you need a time zone. If omitted, your JVM’s current default time zone is implicitly applied. Understand that for any given moment, 👉 the date varies around the globe by time zone; tomorrow in Tokyo Japan is simultaneously yesterday in Toledo Ohio.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
LocalDate today = LocalDate.now( z ) ;
> shows dates of every 15 days
Define the count of days to jump, and how many times to jump.
int daysToJump = 15 , jumps = 5; // Jump to 0, 15, 30, 45, 60 days out.
Define the first day from which we will jump.
LocalDate localDate = today;
Loop for the five iterations we want, adding 0, 15, 30, 45, 60. Add daysToJump
on each.
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 );
}
When run.
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
You said:
>I need o remove Saturdays and Sundays
Yes, we can skip those dates that land on a weekend.
Define the weekend as a set of days of the week. Java comes with seven pre-defined enum objects, one for each day of the week, Monday-Sunday.
Set < DayOfWeek > weekend = EnumSet.of ( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY );
For each jump, add the days to jump, see if that date is on the weekend. If on weekend, ignore that date. If on a weekday, print. Add a test for day-of-week within the loop.
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 );
}
When run:
jumpIndex: 0 | daysJumped: 0 | localDate = 2023-05-25
jumpIndex: 1 | daysJumped: 15 | localDate = 2023-06-09
jumpIndex: 3 | daysJumped: 45 | localDate = 2023-08-23
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论