How to get the date in java

There are various ways to get the date in java.

Date class

java.util.Date The Date class is a class that has been used since the early days of java and is often used to get and display dates. However, be aware that there are many deprecated methods.

Calendar class

java.util.Calendar The Calendar class has more functions than Date and can calculate and refer to dates.

Here, we will introduce how to get the date using the calendar class.

java


import java.util.Calendar;

public class Main {
	public static void main(String args[]){
		//Get date
		Calendar cal = Calendar.getInstance();
                //To count from 0 only for months+1 is easy to understand.
                int month = cal.get(Calendar.MONTH) + 1;
                System.out.println(cal.get(Calendar.YEAR) + month + cal.get(Calendar.DATE));


	}
}

Execution result (for June 28, 2018) 2018628

Recommended Posts

How to get the date in java
How to implement date calculation in Java
How to get date data in Ruby
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
How to get the class name / method name running in Java
How to get Class from Element in Java
[Java] How to get the redirected final URL
[Java] How to get the authority of the folder
How to get the length of an audio file in java
[Java] How to get the current date and time and specify the display format
How to get the current date as a string in yyyyMMdd format
How to get the value after "_" in Windows batch like Java -version
How to get the absolute path of a directory running in Java
How to create your own annotation in Java and get the value
[Java] How to get the URL of the transition source
[Java] How to omit the private constructor in Lombok
[Java] How to get the maximum value of HashMap
Source used to get the redirect source URL in Java
How to use Java HttpClient (Get)
How to get parameters in Spark
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
Get the next business day after the specified date in JAVA
How to derive the last day of the month in Java
How to switch Java in the OpenJDK era on Mac
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
Java reference to understand in the figure
[Java] How to use the hasNext function
[Java] How to use the HashMap class
How to do base conversion in Java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to fix system date in JUnit
How to implement coding conventions in Java
How to embed Janus Graph in Java
[Processing × Java] How to use the class
How to install the legacy version [Java]
[Processing × Java] How to use the function
[Java] How to use the Calendar class
[Java] Get the date with the LocalDateTime class
[Java] Get date information 10 days later using milliseconds in the Date class
How to get the id of PRIMAY KEY auto_incremented in MyBatis
How to solve the unknown error when using slf4j in Java
[Java] How to convert from String to Path type and get the path
How to increment the value of Map in one line in Java
Date manipulation in Java 8
How to get keycloak credentials in interceptor class
How to check the logs in the Docker container
[Java] How to use Thread.sleep to pause the program
[Java] How to get and output standard input
How to display a web page in Java
[Java] (for MacOS) How to set the classpath
How to use the replace () method (Java Silver)
How to get and study java SE8 Gold
[Java] How to substitute Model Mapper in Jackson