extract day from Date

后端 未结 3 1988
忘了有多久
忘了有多久 2021-02-02 12:15

I receive a timestamp from a SOAP service in milliseconds. So I do this:

Date date = new Date( mar.getEventDate() );

How can I extract the day

3条回答
  •  借酒劲吻你
    2021-02-02 12:34

    Update: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.

    See the correct Answer by Ortomala Lokni, using the modern java.time classes. I am leaving this outmoded Answer intact as history.


    The Answer by Lokni is correct.

    Here is the same idea but using Joda-Time 2.8.

    long millisSinceEpoch = mar.getEventDate() ;
    DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ) ;  // Or DateTimeZone.UTC
    LocalDate localDate = new LocalDate( millisSinceEpoch , zone ) ;
    int dayOfMonth = localDate.getDayOfMonth() ;
    

提交回复
热议问题