Set the time of LocalDateTime to a specific time

Date Time API added from Java 8. In the code written using LocalDateTime in this, I sometimes see such code.

Set each field one by one.java


LocalDateTime ldt = LocalDateTime.now().withHour(12).withMinute(0).withSecond(0).withNano(0);

The setting is like "Today's 12 o'clock", but each field of time is set one by one. Well, when I used Calendar, it was basically one by one, so it may have happened in the flow. However, this is inefficient because a new instance is created each time withXxx ().

LocalDateTime # with is used in such cases. That's right.

public LocalDateTime with(TemporalAdjuster adjuster) Returns an adjusted copy of this date / time. It adjusts the date / time and returns a LocalDateTime based on this date / time. Adjustments are made using the specified adjuster strategy object. Please refer to the adjuster documentation to understand what adjustments will be made.

I'm not very familiar with TemporalAdjuster, but here I will simply introduce the following usage.

Set at 12 o'clock in one shot.java


LocalDateTime ldt = LocalDateTime.now().with(LocalTime.of(12, 0));

LocalTime also implements this TemporalAdjuster, so you can pass it to the with method. And it changes the time of the instance of LocalDateTime to the time of the argument.

Recommended Posts

Set the time of LocalDateTime to a specific time
[Java] How to set the Date time to 00:00:00
A memo when you want to clear the time part of the calendar
Make a margin to the left of the TextField
[Java] How to get to the front of a specific string using the String class
Mandels to create an image of the Mandelbrot set
Extract a specific element from the list of objects
[IOS] To allow rotation of only a specific screen
I tried to create a log reproduction script at the time of apt install
Check the dependency of a specific maven artifact in Coursier
How to set chrony when the time shifts in CentOS7
I want to make a specific model of ActiveRecord ReadOnly
Set the time zone in the JVM of your Azure application
[Ruby] How to retrieve the contents of a double hash
[jsoup] How to get the full amount of a document
A memorandum of the FizzBuzz problem
Try to imitate the idea of a two-dimensional array with a one-dimensional array
A solution to the problem of blank spaces at the beginning of textarea
How to set the IP address and host name of CentOS8
[Swift] Set the Style of TableViewCell to something other than Custom
Performance issues during serialization operations related to the use of LocalDateTime
Fix the file name of war to the one set in Maven
I made a gem to post the text of org-mode to qiita
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
Add a time stamp to the JAR file name in Gradle
[chown] How to change the owner of a file or directory
I made a tool to output the difference of CSV file
How to check for the contents of a java fixed-length string
Three Reasons to Frustrate Before the Release of a Web Service
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
When you want to check whether the contents of a property can be converted to a specific type
A memo that was soberly addicted to the request of multipart / form-data
The secret to the success of IntelliJ IDEA
Introduction to java for the first time # 2
The story of forgetting to close a file in Java and failing
[Java] Get the day of the specific day of the week
The road to creating a music game 2
The story of releasing the Android app to the Play Store for the first time.
I can't build if I set the build destination to a simulator with XCode12!
How to determine the number of parallels
How to set an image in the drawable Left / Right of a button using an icon font (Iconics)
Steps to set a favicon in Rails
How to find out the Java version of a compiled class file
How to sort the List of SelectItem
Output of the book "Introduction to Java"
How to change the value of a variable at a breakpoint in intelliJ
How to get the absolute path of a directory running in Java
Define a task to ZIP archive a set of project files in Gradle
The process of introducing Vuetify to Rails
Ability to display a list of products
I translated the grammar of R and Java [Updated from time to time]
Find the difference from a multiple of 10
The road to creating a music game 3
Set up a Java GUI in a separate thread to keep the main
The road to creating a music game 1
How to implement the email authentication function at the time of user registration
[Rails] How to solve the time lag of created_at after save method
(Note) Get a set of dependent library jars with the help of Gradle
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name
What I tried when I wanted to get all the fields of a bean
How to get the ID of a user authenticated with Firebase in Swift