Android studio date now new date
You want to know how to get the current date in Android Studio using Java or Kotlin.
Java:
import java.util.Date;
// Get the current date
Date currentDate = new Date();
// Format the date as desired (e.g., "yyyy-MM-dd")
String formattedDate = DateFormat.getDateInstance().format(currentDate);
Kotlin:
import java.util.Date
// Get the current date
val currentDate = Date()
// Format the date as desired (e.g., "yyyy-MM-dd")
val formattedDate = SimpleDateFormat("yyyy-MM-dd").format(currentDate)
In both cases, you can use the SimpleDateFormat
class to format the date as desired. You can also use the Calendar
class to get the current date and time:
import java.util.Calendar;
// Get the current date and time
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
// Format the date as desired (e.g., "yyyy-MM-dd")
String formattedDate = String.format("%04d-%02d-%02d", year, month + 1, day);
Kotlin:
import java.util.Calendar
// Get the current date and time
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
// Format the date as desired (e.g., "yyyy-MM-dd")
val formattedDate = String.format("%04d-%02d-%02d", year, month + 1, day)
Note that in Kotlin, you need to use the String.format()
method to format the date string, whereas in Java, you can use the SimpleDateFormat
class.