How to get day of the month?

后端 未结 9 1646
南笙
南笙 2020-11-30 05:48

I am trying to retrieve which day of the month it is.

Such as today is August 29,2011.

What i would like to do is just get the days number such as 29, or 30.

9条回答
  •  一整个雨季
    2020-11-30 06:06

    tl;dr

    LocalDate           // Represent a date-only, without time-of-day and without time zone.
    .now()              // Better to pass a `ZoneId` optional argument to `now` as shown below than rely implicitly on the JVM’s current default time zone.
    .getDayOfMonth()    // Interrogate for the day of the month (1-31). 
    

    java.time

    The modern approach is the LocalDate class to represent a date-only value.

    A time zone is crucial in determine the current date. For any given moment, the date varies around the globe by zone.

    ZoneId z = ZoneId.of( "America/Montreal" );
    LocalDate ld = LocalDate.now( z ) ;
    int dayOfMonth = ld.getDayOfMonth();
    

    You can also get the day-of-week.

    DayOfWeek dow = ld.getDayOfWeek();
    

    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor java.sql.* classes.

    Where to obtain the java.time classes?

    • Java SE 8, Java SE 9, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.


    Joda-Time

    UPDATE: The Joda-Time project is now in maintenance mode, and advises migration to the java.time classes. This section left intact for history.

    Using the Joda-Time 2.5 library rather than the notoriously troublesome java.util.Date and .Calendar classes.

    Time zone is crucial to determining a date. Better to specify the zone rather than rely implicitly on the JVM’s current default time zone being assigned.

    DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
    DateTime now = DateTime.now( zone ).withTimeAtStartOfDay();
    int dayOfMonth = now.getDayOfMonth();
    

    Or use similar code with the LocalDate class that has no time-of-day portion.

提交回复
热议问题