import java.text.SimpleDateFormat;
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.getDefault());
String currentDateandTime = sdf.format(new Date());
System.out.println("currentDateandTime:" + currentDateandTime);
output:
currentDateandTime: 03112020_073544
one line code:
/**
* @author lautturi.com
* Java example: get current date in android
*/
import java.util.*;
import java.text.SimpleDateFormat;
import java.time.*;
public class Lautturi {
public static void main(String[] args) {
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
System.out.println("currentDate:" + date);
}
}