1

我是 Android 的初学者,我需要代码来获取今天的号码(从本地日期开始)。像这样:
AvarIday。今天是 10 月 8 日,所以Iday应该等于8.

4

2 回答 2

2

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);
    }
}

输出:

Today, the day of month is 8

如果您想在其他时区获取今天的日期,请使用LocalDate now(ZoneId zone)如下所示:

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);
    }
}

输出:

Today, the day of month is 9
于 2020-09-08T20:55:07.773 回答
1

你可以使用Callendarandget方法

import java.util.*

val cal = Calendar.getInstance()
val dayOfMonth =  cal[Calendar.DAY_OF_MONTH]

println(dayOfMonth) // 8 -> 8th October 2020

getInstance()

获取使用默认时区和语言环境的日历。返回的日历基于具有默认语言环境的默认时区中的当前时间。


已弃用:

import java.util.*

val currentTime = Calendar.getInstance().time
println(currentTime.date) // 8
于 2020-09-08T19:19:34.017 回答