[JAVA] Format the contents of LocalDate with DateTimeFormatter

What is this

Since I read toString of java \ .time \ .LocalTime, I will write an example of using DateTimeFormatter as a memorandum.

It's just a memo writing.

code

Since slf4j is used for log output, pay attention to the dependency when actually running it.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class App {

    private static final Logger logger = LoggerFactory.getLogger(App.class);
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
    private static final DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss.SSS");

    public static void main(String... args) {

        var time = LocalTime.now();
        logger.info(time.toString());
        logger.info(time.format(formatter));

        time = LocalTime.of(14,30);
        logger.info(time.toString());
        logger.info(time.format(formatter));

        logger.info("---------------------");

        var datetime = LocalDateTime.now();
        logger.info(datetime.toString());
        logger.info(datetime.format(formatter2));

        datetime = LocalDateTime.of(2018,8,16,10,55);
        logger.info(datetime.toString());
        logger.info(datetime.format(formatter2));
    }
}

Processing result

16:08:49.562 [main] INFO  App - 16:08:49.555999800
16:08:49.572 [main] INFO  App - 16:08:49.555
16:08:49.572 [main] INFO  App - 14:30
16:08:49.572 [main] INFO  App - 14:30:00.000
16:08:49.572 [main] INFO  App - ---------------------
16:08:49.572 [main] INFO  App - 2018-08-16T16:08:49.572009800
16:08:49.573 [main] INFO  App - 2018/08/16 16:08:49.572
16:08:49.573 [main] INFO  App - 2018-08-16T10:55
16:08:49.573 [main] INFO  App - 2018/08/16 10:55:00.000

Recommended Posts

Format the contents of LocalDate with DateTimeFormatter
Check the contents of params with pry
Verify the contents of the argument object with Mockito
Overwrite the contents of config with Spring-boot + JUnit5
Test the contents of an Excel file with JUnit
[Rails] Check the contents of the object
Replace the contents of the Jar file
[Ruby] Display the contents of variables
Check the contents of the Java certificate store
Memo: [Java] Check the contents of the directory
Prepare the format environment with "Rails" (VScode)
Folding and unfolding the contents of the Recyclerview
Check the processing contents with [rails] binding.pry
About the treatment of BigDecimal (with reflection)
The contents of the data saved by CarrierWave.
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
After all I wanted to preview the contents of mysql with Docker ...
JAVA: jar, aar, view the contents of the file
Customize how to divide the contents of Recyclerview
Manage the version of Ruby itself with rbenv
The story of tuning android apps with libGDX
I want to var_dump the contents of the intent
Prepare the environment of CentOS 8 with Sakura VPS
Specify the default value with @Builder of Lombok
I checked the number of taxis with Ruby
[Docker] How to see the contents of Volumes. Start a container with root privileges.
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[Swift] Get the number of steps with CMP edometer
Format of the log output by Tomcat itself in Tomcat 8
JavaFX --Match the size of ImageView with other nodes
CI the architecture of Java / Kotlin applications with ArchUnit
[JUnit5] Dealing with "the reference of assertEquals is ambiguous"
Access the built-in h2db of spring boot with jdbcTemplate
The story of making a reverse proxy with ProxyServlet
Monitor the internal state of Java programs with Kubernetes
Implement the UICollectionView of iOS14 with the minimum required code.
I summarized the display format of the JSON response of Rails
Check the behavior of Java Intrinsic Locks with bpftrace
Check the result of generic parameter inference with JShell
Java: Use Stream to sort the contents of the collection
Roughly the flow of web application development with Rails.
Control the processing flow of Spring Batch with JavaConfig.
I want to be aware of the contents of variables!
The story of making dto, dao-like with java, sqlite
Replace only part of the URL host with java
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
Basic format of Dockefile
The world of clara-rules (5)
Automate format with maven-formatter
The idea of quicksort
Format JSON with org.json
I want to recreate the contents of assets from scratch in the environment built with capistrano
The idea of jQuery
I took a peek at the contents of Java's HashMap
A story packed with the basics of Spring Boot (solved)
Check the operation of two roles with a chat application
Delete all the contents of the list page [Ruby on Rails]