The task:

The programmer's day is celebrated on the 255th day of the year (January 1 is considered a zero day). It is required to write a program that determines the date (month and day of the Gregorian calendar), on which Programmer's Day falls on a given year.

The date must be in the format DD / MM / YYYY , where DD is the number, MM is the month number (01 - January, 02 - February), YYYY - the year in decimal

And enter an integer from 1 to 9999 inclusive, year of our era.

Here is the code:

Scanner in = new Scanner(System.in); int YEAR = in.nextInt(); SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Calendar gCal = new GregorianCalendar(YEAR, 0, 0); gCal.add(Calendar.DAY_OF_YEAR, 256); String dayP = sdf.format(gCal.getTime()); System.out.println(dayP); 

I can not understand the problem. In the compiler, everything goes as I need, in Leap years, the day 12/09/2000, in others it is 13/09/2100.

But the site does not accept this code and does not pass the 6th test, I do not understand what data is not read. Maybe problems with Calendar and SDF ? Just a little experience with them, and I took advantage of it.

  • So everything is correct, 2000 is a leap year, and 2100 is not. - andy.37
  • The site indicates that it does not pass all tests, where something is wrong, hmm - TheOldManPickAssO
  • use java8, and if there is no such possibility, then - joda time, everything is just LocalDate.of (2018, 01, 01) .plusDays (255); - Dmitriy
  • Hmmm, what about Java8? - TheOldManPickAssO
  • and in java 8 there is a java.time package, in fact it is joda time just already out of the box. Well, of course, I mean 8 and above - Dmitry

2 answers 2

 int y = in.nextInt(); boolean isLeap = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0); int d = isLeap ? 12 : 13; System.out.printf("%02d/09/%04d", d, y); 
  • Who can explain to me ("% 02d / 09 /% 04d", d, y), how this line works? I know from my knowledge that the type is first specified and then separated by commas, as here, but why is% 02? and% 04? - TheOldManPickAssO
  • The answer helped me. Thank you, but still do not understand that line. Explain, I will be grateful - TheOldManPickAssO
  • %04d - output int in 4 characters, complementing the left with zeros. - Qwertiy
  • Wow, so much, thank you. and "d" what does it mean? How to find out more about it? What is this operation? - TheOldManPickAssO
  • d means int in decimal notation (decimal). - Qwertiy

GregorianCalendar by default will arrive as the Gregorian calendar only after the year 1582 .

You can use the setGregorianChange method and the setCalendar method to change the behavior:

  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); GregorianCalendar gCal = new GregorianCalendar(); gCal.setGregorianChange(new Date(Long.MIN_VALUE)); gCal.set(YEAR, 0, 0); gCal.add(Calendar.DAY_OF_YEAR, 256); sdf.setCalendar(gCal); String dayP = sdf.format(gCal.getTime()); System.out.println(dayP);