fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.time.* ;
  8. import java.time.format.* ;
  9. import java.time.temporal.* ;
  10. import java.time.chrono.* ;
  11. import java.time.zone.* ;
  12.  
  13. /* Name of the class has to be "Main" only if the class is public. */
  14. class Ideone
  15. {
  16. public static void main (String[] args) throws java.lang.Exception
  17. {
  18.  
  19. LocalDate firstDayOfPayPeriod = LocalDate.of( 2020 , Month.OCTOBER , 23 );
  20. LocalDate firstDayOfSuccessivePayPeriod = LocalDate.of( 2020 , 11 , 7 );
  21.  
  22. String input = "2020-10-31";
  23. LocalDate dateInQuestion = null;
  24. try
  25. {
  26. dateInQuestion = LocalDate.parse( input );
  27. }
  28. catch ( DateTimeParseException e )
  29. {
  30. // Handle faulty input.
  31. e.printStackTrace();
  32. }
  33.  
  34. // Validate dates.
  35. Objects.requireNonNull( firstDayOfPayPeriod );
  36. Objects.requireNonNull( firstDayOfSuccessivePayPeriod );
  37. Objects.requireNonNull( dateInQuestion );
  38. if ( ! firstDayOfPayPeriod.isBefore( firstDayOfSuccessivePayPeriod ) )
  39. {
  40. throw new IllegalStateException( "…" );
  41. }
  42. if ( dateInQuestion.isBefore( firstDayOfPayPeriod ) )
  43. {
  44. throw new IllegalStateException( "…" );
  45. }
  46. if ( ! dateInQuestion.isBefore( firstDayOfSuccessivePayPeriod ) )
  47. {
  48. throw new IllegalStateException( "…" );
  49. }
  50.  
  51.  
  52. long payPerDay = 100;
  53. long partialPay = 0;
  54.  
  55. LocalDate localDate = firstDayOfPayPeriod;
  56. while ( localDate.isBefore( firstDayOfSuccessivePayPeriod ) )
  57. {
  58. if ( localDate.isBefore( dateInQuestion ) )
  59. {
  60. partialPay = ( partialPay + payPerDay );
  61. }
  62.  
  63. // Set up the next loop.
  64. // Notice that java.time uses immutable objects. So we generate a new object based on another’s values rather than alter (mutate) the original.
  65. localDate = localDate.plusDays( 1 ); // Increment to next date.
  66. }
  67.  
  68. System.out.println( "Partial pay earned from firstDayOfPayPeriod " + firstDayOfPayPeriod + " to dateInQuestion " + dateInQuestion + " is " + partialPay );
  69.  
  70.  
  71.  
  72. }
  73. }
Success #stdin #stdout 0.16s 38584KB
stdin
Standard input is empty
stdout
Partial pay earned from firstDayOfPayPeriod 2020-10-23 to dateInQuestion 2020-10-31 is 800