2

I couldnt find a symbol for that in SimpleDateFormat

How can I get the current day's localized name?

For example: Monday: 1. day of week ; Tuesday: 2.day, wednesday:3.day .....

I want to get Presentation Number "1" instead of Monday ...

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
metemet06
  • 1,472
  • 4
  • 18
  • 30
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most 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 (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Dec 11 '18 at 03:39

2 Answers2

1

As per other questions, you don't need SimpleDateFormat to get the numeric day of the week - that is provided by Calendar directly via the DAY_OF_WEEK field (which goes from 1 to 7 where 1 is SUNDAY and 7 is SATURDAY):

Calendar today = Calendar.getInstance();
int dayOfWeek = today.get(Calendar.DAY_OF_WEEK);
// Day of Week is a number between 1 and 7 where 1 is Sunday.
int dayOfWeekMondayFirst = (dayOfWeek + 5) % 7 + 1;
Community
  • 1
  • 1
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I want to get number "1" instead of monday for example – metemet06 May 05 '13 at 18:03
  • @metemet06 - I've updated my answer to give the numeric day of week, rather than the textual. Note that the default is Sunday as 1, rather than Monday. I've included the translation to Monday=1 as well. – ianhanniballake May 05 '13 at 18:19
  • Thanks, it really helped. I was just looking in SimpleDateFormat – metemet06 May 05 '13 at 18:27
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most 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 (<26) in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP). See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Dec 11 '18 at 03:14
1

tl;dr

For the number 1-7, meaning Monday-Sunday, for today:

LocalDate.now().getDayOfWeek().getValue() 

For the localized name of the day of the week:

LocalDate                          // Represent a date-only, without time-of-day and without time zone.
.now(                              // Get today’s current date as seen in the wall-clock time used by the people of a particular region (a time zone).
    ZoneId.of( "Africa/Tunis" )    // Specify time zone.
)                                  // Returns a `LocalDate`.
.getDayOfWeek()                    // Returns one of seven `DayOfWeek` enum objects.
.getDisplayName(                   // Localize the name of the day-of-week.
     TextStyle.FULL ,              // How long or abbreviated should the localized string be.
     Locale.UK                     // Specify a `Locale` to determine the human language and cultural norms to use in localizing.
)                                  // Returns a string.

Monday

java.time

The modern approach uses the java.time classes that long ago supplanted the terrible legacy date-time classes such as SimpleDateFormat.

To get the day-of-week today, we need the date.

LocalDate

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

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 during runtime(!), 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 2-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.

DayOfWeek

The DayOfWeek enum pre-defines a set of seven objects, one for each day of the week.

Ask the LocalDate object for its DayOfWeek.

DayOfWeek dow = ld.getDayOfWeek() ;

Ask the DayOfWeek object to automatically localize its name. The DayOfWeek::getDisplayName method translates the name of the day into any human language specified by a Locale such as Locale.US or Locale.CANADA_FRENCH.

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

lundi

Or, in US English.

String output = dow.getDisplayName( TextStyle.FULL , Locale.US ); 

Monday

To get the number of the day-of-week, where Monday-Sunday is 1-7 per the ISO 8601 standard, ask the DayOfWeek enum object for its value.

int dowNumber = dow.getValue() ;  // 1-7 for Monday-Sunday.

To get the number 1-7 as part of a larger formatting pattern use e or c as directed in the DateTimeFormatter class.


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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154