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