
In order to get the current date (without time zone) in ISO-8601 format (yyyy-MM-dd) you can make use of the LocalDate class from the Java 8 java.time package.
In the same way, to get the current time (without time zone) in ISO-8601 format (HH:mm:ss.S) format you can make use of the LocalTime class from the same java.time package.
package org.code2care;
import java.time.LocalDate;
import java.time.LocalTime;
/**
* Local date in yyyy-MM-dd format
* using Java 8 LocalDate now() method
*
* Local time in HH:mm:ss.S format
* using Java 8 LocalTime nome() method
*
*/
public class JavaDateTimeExamples {
public static void main(String[] args) {
//Local Date
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
//Local Time
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
}
}
Output:
2022-04-24
12:57:08.710499
And yes! If you want to get both Date and Time, you can make use of the class LocalDateTime from Java 8 java.time package, you will get the date-time in yyyy-MM-ddTHH:mm.s.S format
package org.code2care;
import java.time.LocalDateTime;
/**
* Local date & time using java 8
* LocalDateTime class now() method
*
*/
public class JavaDateTimeExamples {
public static void main(String[] args) {
//Local Date and Time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
}
}
Output:
2022-04-24T12:08:46.721044
More Posts related to Java,
- How to install Java 11 on Mac
- Get Client IP address from HTTP Response in Java
- SharePoint Open in the client application document opens in browser
- How to verify if java is installed on the computer and get version detail
- Java - Check if array contains the value
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- How to run Java Unit Test cases with Apache Maven?
- What Java version is used for Minecraft 1.18
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- Convert Instant timestamp into LocalDateTime Java Code Example
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Create simple struts2 project using maven commands
- Java - PatternSyntaxException
- List of Online Java compiler with console
- Minecraft Java Edition
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Java XML-RPC 3.1.x based web service example
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java 8 foreach loop code examples
- Java -Day of the week using Java 8 DayOfWeek Enum
- Java 8 - Convert List to Map Examples
- Java: TimeZone List with GMT/UTC Offset
- list of jars required for hibernate 4.x.x
More Posts:
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier. - Git
- Get Wifi Details : Android Programming - Android
- Notepad++ display files on tab bar as horizontal instead of vertical - NotepadPlusPlus
- How to know list of images available on your device - Docker
- SharePoint installation - Appfabric installation failed because installer MSI returned with error code:1603 - SharePoint
- Android Studio 4.2 Canary 1 now available - Android-Studio
- How to Detect Phone Shakes Android Programming - Android
- Pass data between two Android Activities and access it using Intent - Android