Working with dates

  • 10
  • Locked
The Nebula-1 station receives spaceships only on odd days of the standard earth year (i.e. days when there have been an odd number of days since the beginning of the year). Let's find out if we can party on Nebula-1 today. To do this, we'll implement the isDateOdd(String date) method, which returns true if the number of days since the beginning of the year is odd, and otherwise returns false.
You can't complete this task, because you're not signed in.
Comments (14)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Andrew Evans
Level 17 , San Jose, Canada
15 February 2022, 03:59
Geniunely curious why this doesn't pass: I won't post all the code. But assume that the heavy lifting is done and the dates are parse correctly. LocalDate d1 = LocalDate.parse(str, formatter); LocalDate d2 = LocalDate.parse(newYears, formatter); long daysBetween = java.time.temporal.ChronoUnit.DAYS.between(d2, d1); return daysBetween % 2 == 0 ? true : false;
Andrew Evans
Level 17 , San Jose, Canada
15 February 2022, 04:00
ChronoUnit.Days.between will give the number of days between. Then the logic is super simple so what gives?
ImDevin
Level 15 , Old Town, United States
5 May 2021, 03:22
the actual solution was not long or complicated, but there are so many different ways to get at this problem, it was very time-consuming. Solved it by using Date to store the parameter and using it to set the Calendar time to get the DAY_OF_YEAR. Gonna have to do more task to get better at doing the date, calendar thing. Happy coding! :)
Tommy
Level 10 , Phoenix, AZ, United States
21 February 2021, 01:59
Here is a tutorial on parsing String to Date that I found incredibly useful in solving this project: https://howtodoinjava.com/java/date-time/java-parse-string-to-date/ Couple that with CodeGym tutorial on this exact problem: https://codegym.cc/quests/lectures/questsyntax.level08.lecture04
Samuel Michael
Level 16 , Stafford, United States
Expert
5 January 2021, 18:24
JANUARY 1 2000 = true JANUARY 2 2020 = false If the requirement is to return true if the number of days since the beginning of the year is odd, then January 2 2020 should return true because it is 1 (odd) day since the beginning of the year. January 1 2020 is an understandable exception.
ziv fisher
Level 9 , Petah Tikva, israel
17 October 2020, 08:24
Determine how many days have passed since the beginning of the year: https://codegym.cc/quests/lectures/questsyntax.level08.lecture04
Vishal Gautam
Level 13 , Bangalore, India
13 October 2020, 08:04
Tip: Clone the date object that you initialized by parsing the String parameter. Date newYear = new Date(passedDate.clone().toString()); Set the month and date to 0(Jan) and 1. No need to set the year. Number of days passed since new year = differenceInMilliseconds/(1000*60*60*24) + 1
MoJo
Level 22 , Cairns, Australia
31 August 2020, 09:52
Hint: you can set a date to the beginning of the year and increase this in a loop by BeginningOfTheYearDate.setDate(BeginningOfTheYearDate.getDate()+1) . If you do so until it is the same as the input date and keep track of the times you increased the date you can easily determine if the days passed are odd or even.
Hannah Software Architect
30 July 2020, 10:50
I am currently working on one of your exercises and somehow the Calendar.MONTH isn't working. The Compiler is telling me, that there is no constructor for GregorianCalendar(int,java.lang.String,int). Only java.util.GregorianCalendar.GregorianCalendar(int,int,int). So I guess I will have to write a little method to transform the Month-Word into the corresponding number... Maybe someone can tell me, why Calendar.MONTH isn't working? Am I needed to import another class? Currently I have util.Date; util.Calendar and util.GregorianCalendar; Update: because of the requirement that the Solution class must have two methods, I now have to take everything I have outsorced into my transformMonth method and have to throw it in the isDateOdd method, and everything will be totally incomprehensible
Petros
Level 23 , Columbia, United States
26 July 2020, 02:47
Brutal. Couldn't solve it on my own. Had to look at the help section.