• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get last day of the century result like 31-dec-2099

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

 How to get last day of the century result like 31-dec-2099?

Regards
Naren
 
Rancher
Posts: 517
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java SE 8's java.time has API has classes to create a date and java.time.format has classes to format the created date.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What makes you think that is the last day of the century? You may find there is a way to go from century to century in the package PS told you about (more details in the Java™ Tutorials), but you may find you can only go to the correct century end (31-dec-2100).
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:...but you may find you can only go to the correct century end (31-dec-2100).

Please explain.
 
Saloon Keeper
Posts: 15491
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Counting years is 1-based.

The year 100 is the last year of the first century, not the first year of the second century.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
imagine we started over with the calendar.  Today would be Jan 1st, year...1.  the number tells you what year you are IN, not how many years have gone PAST.

So on Jan 1st of the year 100, you've only been through 99 years...you still have a full year to go.  Now just carry that forward, and you'll see why the end of the 21st century will be on Dec 31, 2100.
 
Greenhorn
Posts: 6
Chrome Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:imagine we started over with the calendar.  Today would be Jan 1st, year...1.  the number tells you what year you are IN, not how many years have gone PAST.

So on Jan 1st of the year 100, you've only been through 99 years...you still have a full year to go.  Now just carry that forward, and you'll see why the end of the 21st century will be on Dec 31, 2100.




You're absolutely right, but in most people's mind, a millennium/century/decade starts on the year that ends with 0. That doesn't mean they're right though.  

What it means is that the question needs to be asked when developing the solution: do you need the mathmatically/historically correct last day of the century, or do you need what most people would call the last day of the century?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Counting years is 1-based. . . .

All human counting is 1‑based, isn't it?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Asher Max Schweigart wrote:. . . but in most people's mind, a millennium/century/decade starts on the year that ends with 0. . . .

You aren't going to believe this, but I don't remember the 19th century. But people probably knew that it ended with a year starting 19. That was the only year in the 19th century with 19 in. 2000 was the only year in the 20th century with 20 in.

do you need the mathmatically/historically correct last day of the century, or do you need what most people would call the last day of the century?

Not quite the right question. We are looking at code. Consider asking,
  • 1: Is there a method for finding the start or end of the century automatically?
  • 2: If there is, does it give the correct end year for the century?
  •  
    fred rosenberger
    lowercase baba
    Posts: 13089
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Stephan van Hulst wrote:Counting years is 1-based. . . .

    All human counting is 1‑based, isn't it?



    umm...nope.  How old are you?  If you were born on Jan 1st, 1990, you are zero years old until you reach your first birthday a year later.  I would call that zero based.
     
    narendra enamala
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi All
      thanks for your replies
       I want I want to get either last day of the century or I need 1000 years later like 31-dec-2999
    that is my requirement.

    Thanks in advance
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    fred rosenberger wrote:. . . you are zero years old until you reach your first birthday . . .

    That is an exception. But most parents say they have a six‑hour‑old baby, or a six‑day‑old baby, or a six‑week‑old baby, or a six‑month‑old baby.
     
    Marshal
    Posts: 28177
    95
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    This gets you what you want -- right?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 15491
    363
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    narendra enamala wrote:I want I want to get either last day of the century or I need 1000 years later like 31-dec-2999


    Do you mean millennium? Even then, as we've mentioned before in this thread, December 31st 2999 is not the last day of a millennium. December 31st 3000 is. If you really want 2999, then you have to subtract a year.

    Anyway, to get the last day of the current century, do the following:

  • Get the first day of the first century using LocalDate.ofYearDay().
  • Get today using LocalDate.now().
  • Get the number of centuries in between by getting the Period.between() the first day of the first century and today, getting the number of years from that period, and dividing it by 100.
  • Get the first day of the next century by taking the first day of the first century plus() the number of ChronoUnit.CENTURIES you found earlier, plus one more century.
  • Get the last day of the current century by taking the first day of the next century minus one day.

  • Finally, if you REALLY want, you can subtract another year if that's the requirement, but it doesn't really make sense.
     
    Rancher
    Posts: 4801
    50
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:You aren't going to believe this, but I don't remember the 19th century. But people probably knew that it ended with a year starting 19. That was the only year in the 19th century with 19 in. 2000 was the only year in the 20th century with 20 in.



    This takes me back to the whole millennium celebrations and the argument they were doing it all a year early.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic