0

I am trying to extract the day of month of today's date. I have this

DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = new Date();
JOptionPane.showMessageDialog(null, date.getDay());

but when the message dialog appears it show the number 5 and today it's the 8th. How can I set it to show what day of the month it is?

Andres Rivera
  • 97
  • 2
  • 7
  • 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 20 '18 at 04:58

4 Answers4

1

date.getDay() returns the day of the week. sunday is 0 and similarly saturday is 6.

Please see the java docs

As per the comment given below

Calendar cal = Calendar.getInstance();
    int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);

    String dayOfMonthStr = String.valueOf(dayOfMonth);
    System.out.println(dayOfMonthStr);
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • Yeah I saw that in Java API, but is there a way to show the actual day? – Andres Rivera Nov 09 '13 at 05:34
  • actual day means what? – SpringLearner Nov 09 '13 at 05:35
  • Sorry, It means like today the 8th or tomorrow the 9th etc.. Not Monday, Tuesday etc.. I hope I'm explaining myself well. – Andres Rivera Nov 09 '13 at 05:36
  • @AndresRivera you want actually day of the month,see my updated answer – SpringLearner Nov 09 '13 at 05:38
  • So it's not possible with the Date class? I have to use Calender right? – Andres Rivera Nov 09 '13 at 05:40
  • @AndresRivera getDate() in date returns the day of the month but its deprecated.I would suggest you not to use deparacted methods.Please go for calender – SpringLearner Nov 09 '13 at 05:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40859/discussion-between-javabeginner-and-andres-rivera) – SpringLearner Nov 09 '13 at 05:44
  • @AndresRivera, `So it's not possible with the Date class?`, yes its possible. – camickr Nov 09 '13 at 05:49
  • @camickr I have replied him by using getDate() but its deprecated – SpringLearner Nov 09 '13 at 05:51
  • I wasn't suggesting to use getDate(), see my answer. – camickr Nov 09 '13 at 05:52
  • If I wanted to use the getDate() how would I get it to show the actual day of the month? – Andres Rivera Nov 09 '13 at 06:10
  • @AndresRivera date.getDate() will give you date of the month – SpringLearner Nov 09 '13 at 06: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 20 '18 at 04:57
0

Try this out.

JOptionPane.showMessageDialog(null, date.getTime());
Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

You started to use the SimpleDateFormat class, but didn't do anything with it. Try:

System.out.println( new SimpleDateFormat("EEEE").format( new Date() ) );
System.out.println( new SimpleDateFormat("d").format( new Date() ) );
camickr
  • 321,443
  • 19
  • 166
  • 288
0

tl;dr

LocalDate.now()             // Capture the current date as seen in the wall-clock time used by the people of a certain region, that region represented by the JVM’s current default time zone.
         .getDayOfMonth()   // Extract the day-of-month. Returns an `int`. 

java.time

extract the day of month of today's date

The modern approach uses the java.time classes that supplant the troublesome old date-time classes bundled with the earliest versions of Java.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment, so your results may vary. Better to specify your desired/expected time zone explicitly as an argument.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" ) ;  
LocalDate today = LocalDate.now( z ) ;

If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.

ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.

Interrogate the LocalDate for its day-of-month.

int dayOfMonth = today.getDayOfMonth() ;

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.

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

Where to obtain the java.time classes?

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, with the team advising migration to the java.time classes. I am leaving this section intact as history.

In Joda-Time 2.3 in Java 7…

org.joda.time.DateTime theEighth = new org.joda.time.DateTime( 2013, 11, 8, 18, 0 ); // Default time zone.
System.out.println( "theEighth: " + theEighth );
System.out.println( "dayOfMonth of theEighth: " + theEighth.dayOfMonth().getAsText() );

When run…

theEighth: 2013-11-08T18:00:00.000-08:00
dayOfMonth of theEighth: 8
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154