Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Draft saved
Draft discarded
Cancel
11
  • 1
    Why do you want to change the method? What is it you're actually trying to do for which you think you need to do this? Commented Mar 3, 2024 at 7:41
  • 6
    The right way to have a different "current time" in non-production environments (for example, for testing what will happen at some future date) is never to use LocalDateTime.now(), but always LocalDateTime.now(Clock clock) instead, and inject a suitable Clock implementation into any class that needs the current time. Are you sure that "How can I modify the core api in Java" is the question you actually want to ask? Commented Mar 3, 2024 at 7:46
  • @DawoodibnKareem: In a non-production environment, another option is to set the system clock to any time you want. (e.g. inside a VM, but outside the JVM.) Or perhaps with LD_PRELOAD with a library that intercepts libc function calls by the JVM, so clock_gettime will return different times. That would let LocalDateTime stuff be different from the mod times on files your program writes. Commented Mar 3, 2024 at 8:04
  • purpose : change the implementation of get system time in non-prd-env,and the developer do nothing,and can not change the system clock. Commented Mar 3, 2024 at 8:15
  • 1
    @PeterCordes - Well yea ... but that potentially effects the entire system. It will break some things (e.g. those that depend on clocks being in sync with other machines), and make other things behave in undesirable ways; e.g. timestamps in log messages for the entire environment. Commented Mar 3, 2024 at 8:16

lang-java

AltStyle によって変換されたページ (->オリジナル) /