20

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 of the month from date, since methods such as Date::getDay() are deprecated?

I am using a small hack, but I do not think this is the proper way to obtain day-of-month.

SimpleDateFormat sdf = new SimpleDateFormat( "dd" );
int day = Integer.parseInt( sdf.format( date ) );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Daniel
  • 2,001
  • 5
  • 17
  • 11
  • 1
    Use [the Java Calendar class for this](http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html). – zs2020 Apr 12 '10 at 03:19
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 08 '18 at 05:09

3 Answers3

36

Use Calendar for this:

Calendar cal = Calendar.getInstance();
cal.setTime(mar.getEventDate());
int day = cal.get(Calendar.DAY_OF_MONTH);
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
cletus
  • 616,129
  • 168
  • 910
  • 942
2

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() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Given the Date constructor used in the question

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

The method mar.getEventDate() returns a long that represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Java 8 and later

In Java 8, you can extract the day of the month from this value, assuming UTC, with

LocalDateTime.ofEpochSecond(mar.getEventDate(),0,ZoneOffset.UTC).getDayOfMonth();

Note also that the answer given by cletus assume that mar.getEventDate() returns a Date object which is not the case in the question.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Apr 07 '18 at 23:57