简体   繁体   中英

How can I get present months?

I tried

Calendar calendar= Calendar.getInstance();
System.out.println("use with command get time \"calendar.getTime()\"           : "+calendar.getTime());
System.out.println("use with command get month \"calendar.get(Calendar.MONTH)\": "+calendar.get(Calendar.MONTH));

but I can't get the right month.

this is my output

How can I get the current month? Thanks for reading and sorry for my bad English.

java.time.LocalDate

You can use below to get month value in int or string

LocalDate.now().getMonthValue();
LocalDate.now().getMonth();

You can also use the SimpleDateFormat method to get the current date and time.

Here see in my function I get the current date and time for your reference

private void getTime(TextView dateTime) {
    Date date = new Date();
    @SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EE, d MMM yyyy HH:mm:ss aaa");
    dateTime.setText(simpleDateFormat.format(date));
}

For more information, you can refer to the official android documentation here

Here are some examples from official documentation. Reference Link

SimpleDateFormat sdf = new SimpleDateFormat("MM");
Date date = new Date();
Log.d("MyTag", sdf.format(date));

Here MM represents months in integer form (dec ~ 12) Here MMM represents months in alphabets (Dec)

The Answer by Sriram is correct, and provides the modern solution using java.time.LocalDate class. Here are a few more details.

Time zone

The LocalDate#now method implicitly uses the JVM's current default time zone. Time zone is critical because for any given moment the time, and therefore the date, can vary around the globe by zone. It can be “tomorrow” in Tokyo Japan while simultaneously be “yesterday” in Toledo Ohio US.

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
LocalDate today = LocalDate.now( z ) ;

If you omit passing the ZoneId object, the JVM'S current default time zone is automatically applied. This has the same effect as explicitly passing the default.

LocalDate today = LocalDate.now( ZoneId.systemDefault() ) ;

I recommend the explicit approach to make your intentions crystal-clear.

Month enum

The method LocalDate#getMonth returns a Month enum object.

Month month = LocalDate.now().getMonth() ;

If you want to generate text to represent that value, you can automatically localize.

Locale locale = Locale.CANADA_FRENCH ;
String output = month.getDisplayName( TextStyle.FULL , locale ) ;

See this code run at Ideone.com .

décembre

You can access the JVM's current default locale by calling Locale.getDefault() .

Android

Android 26+ carries an implementation of the java.time classes.

For earlier Android, use the latest tooling to access most of the java.time functionality via “API desugaring”.

If you look at the source code for class java.util.Calendar , you will find the following constant:

    /**
     * Value of the {@link #MONTH} field indicating the
     * twelfth month of the year in the Gregorian and Julian calendars.
     */
    public static final int DECEMBER = 11;

Hence calendar.get(Calendar.MONTH)) correctly returns 11 (eleven).

However, Android 8 (API level 26) added class LocalDate which should be used instead of class Calendar . Note that class Calendar contains both date and time whereas class LocalDate contains the date only. Class LocalDateTime contains both a date and a time. There is also class LocalTime .

According to the documentation for [static] method now , in class LocalDate :

Obtains the current date from the system clock in the default time-zone.

If you want the current month, you can call method getMonthValue which...

Gets the month-of-year field from 1 to 12.

Alternatively, you can call method getMonth which...

Gets the month-of-year field using the Month enum.

So your code should be:

java.time.LocalDate calendar = java.time.LocalDate.now();
System.out.println("calendar           : " + calendar);
System.out.println("calendar.getMonth(): " + calendar.getMonth());

which produces the following output:

calendar           : 2022-12-30
calendar.getMonth(): DECEMBER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM