I am a beginner in Android and I need code to get today number (from local date).
Like this way:
A var is Iday. And today is 8th October so Iday should be equal to 8.
How to get date in Android (Kotlin)
1.1k Views Asked by e88990 At
2
There are 2 best solutions below
0
On
LocalDate#getDayOfMonth
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
// Today at the default time-zone (your JVM's time-zone)
LocalDate date = LocalDate.now();
// Day of month today
int dayOfMonth = date.getDayOfMonth();
// Display
System.out.println("Today, the day of month is " + dayOfMonth);
}
}
Output:
Today, the day of month is 8
If you want to get today's date at some other time zone, use LocalDate now(ZoneId zone) as shown below:
import java.time.LocalDate;
import java.time.ZoneId;
public class Main {
public static void main(String[] args) {
// Today at Melbourne
LocalDate date = LocalDate.now(ZoneId.of("Australia/Melbourne"));
// Day of month today
int dayOfMonth = date.getDayOfMonth();
// Display
System.out.println("Today, the day of month is " + dayOfMonth);
}
}
Output:
Today, the day of month is 9
You can use
Callendarandgetmethod.getInstance():Deprecated: