Learn to get the current date and time and output it in various formats. In this article, I am using LocalDateTime and DateTimeFormatter to output date and time.
DateTimeFormatter, LocalDateTime is a new class introduced in JAVA8.
It replaces SimpleDateFormat which was used previously.
Get current date/time, output in default format
LocalDateTime.now()
gets the current date and time.
LocalDate.now
only gets the current date.
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Example {
public static void main(String args[]) {
LocalDateTime dateAndtime = LocalDateTime.now();
LocalDate onlyDate = LocalDate.now();
System.out.println("Current date and time: " + dateAndtime);
System.out.println("Current date: " + onlyDate);
}
}
The output will be in the following format:
Current date and time: 2019-03-23T00:05:54.608
Current date: 2019-03-23
For reference, to create LocalData and LocalDateTime of a specific time, you can use of()
.
LocalDate.of(2019, 3, 22) // 2019년 3월 22일
LocalDateTime.of(2019, 3, 22, 10, 10, 10) // 2019년 3월 22일 10시 10분 10초
Output in predefined format
You can use DateTimeFormatter
to output in different formats.
ISO_DATE
is a predefined format provided by the library. Other than that, there are many other things.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Example {
public static void main(String args[]) {
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
String formatted = current.format(formatter);
System.out.println("Current: " + formatted);
}
}
Current: 2019-03-22
The formats of DateTimeFormatter provided by the library are as follows.
constant | output example |
---|---|
ISO_DATE_TIME | 2019-03-22T23:56:36.4 |
ISO_LOCAL_DATE | 2019-03-22 |
ISO_LOCAL_TIME | 23:56:36.4 |
ISO_LOCAL_DATE_TIME | 2019-03-22T23:56:36.4 |
ISO_DATE | 2019-03-22 |
ISO_TIME | 23:56:36.4 |
output in other formats
You can use DateTimeFormatter.ofPattern()
to output in any format you want. Instead, you need to create a pattern and pass it as an argument.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Example {
public static void main(String args[]) {
LocalDateTime current = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분 ss초");
String formatted = current.format(formatter);
System.out.println("Current: " + formatted);
}
}
Current: 2019년 03월 22일 23시 46분 36초
You can use the following patterns in DateTimeFormatter.ofPattern()
.
pattern | Example |
---|---|
yyyy-MM-dd | “2019-07-04” |
dd-MMM-yyyy | “04-July-2019” |
dd/MM/yyyy | “04/07/2019” |
yyyy-MM-dd'T'HH:mm:ssZ | “2019-07-04T12:30:30+0530” |
h:mm a | “12:00 PM” |
MM dd, yyyy | "January 10, 2019" |
Clean up
We saw how to create LocalDateTime and LocalDate and output them in various ways.
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)