171

I know of date formats such as
"yyyy-mm-dd" -which displays date in format 2011-02-26
"yyyy-MMM-dd"-which displays date in format 2011-FEB-26

to be used in eg:

SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy/MMM/dd ");

I want a format which would help me display the day of the week like 2011-02-MON or anything. I just want the day of the week to be displayed in characters with the month and the year. Can you tell me of a format like this?

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
rogerstone
  • 7,541
  • 11
  • 53
  • 62
  • 1
    How about just using `dd` as format? – Johan Sjöberg Feb 25 '11 at 20:00
  • 3
    I'm sure the API docs (http://download.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html), would be happy to provide you an answer. – M. Jessup Feb 25 '11 at 20:02
  • 1
    what do you mean "day" - day of week, day of month. In your example the whole date is displayed. Please show the exact input and output you want. – Bozho Feb 25 '11 at 20:03
  • 2
    At a general advise, better use the `-` notation only with dates in the ISO format (i.e. year-month-day). The dash was elected there since it was not used for any other date formats at the time, to avoid confusion, and now everyone is using his custom ordering with the dash ... – Paŭlo Ebermann Feb 25 '11 at 20:17

6 Answers6

424

This should display 'Tue':

new SimpleDateFormat("EEE").format(new Date());

This should display 'Tuesday':

new SimpleDateFormat("EEEE").format(new Date());

This should display 'T':

new SimpleDateFormat("EEEEE").format(new Date());

So your specific example would be:

new SimpleDateFormat("yyyy-MM-EEE").format(new Date());
Piyush
  • 1,744
  • 1
  • 15
  • 28
Nathan Feger
  • 19,122
  • 11
  • 62
  • 71
  • 6
    Beautiful solution! What I was looking for was: Monday, 04/04/2016. So in my case, `new SimpleDateFormat("EEEE, dd/MM/yyyy");` if it helps someone in the future. – AuroMetal Apr 01 '16 at 14:43
  • 2
    OK, it works. `EEEE` displays whole names of day of week - i.e. 'Monday', 'Tuesday' etc. – kosiara - Bartosz Kosarzycki Jun 04 '18 at 09:05
  • I hadn't really considered that this site would even be around, let alone java would finally get around to bailing on Date. It does technically answer the OPs question. However, I can probably add a new section on how the new java.time stuff works – Nathan Feger Jun 20 '19 at 21:28
  • nice nice... EEEE is the complete written day's name. – gumuruh Aug 24 '22 at 06:12
20

Yep - 'E' does the trick

http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-E");
System.out.println(df.format(date));
toolkit
  • 49,809
  • 17
  • 109
  • 135
7
SimpleDateFormat sdf=new SimpleDateFormat("EEE");

EEE stands for day of week for example Thursday is displayed as Thu.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
user2824143
  • 87
  • 1
  • 2
7

tl;dr

LocalDate.of( 2018 , Month.JANUARY , 23 )
         .format( DateTimeFormatter.ofPattern( “uuuu-MM-EEE” , Locale.US )  )

java.time

The modern approach uses the java.time classes.

LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;

Note how we specify a Locale such as Locale.CANADA_FRENCH to determine the human language used to translate the name of the day.

DateTimeFormatter f = DateTimeFormatter.ofPattern( “uuuu-MM-EEE” , Locale.US ) ;
String output = ld.format( f ) ;

ISO 8601

By the way, you may be interested in the standard ISO 8601 week numbering scheme: yyyy-Www-d.

2018-W01-2

Week # 1 has the first Thursday of the calendar-year. Week starts on a Monday. A year has either 52 or 53 weeks. The last/first few days of a calendar-year may land in the next/previous week-based-year.

The single digit on the end is day-of-week, 1-7 for Monday-Sunday.

Add the ThreeTen-Extra library class to your project for the YearWeek 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.

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
3

Use "E"

See the section on Date and Time Patterns:

JavaDocs for SimpleDateFormat

DHall
  • 393
  • 2
  • 13
2

I know the question is about getting the day of week as string (e.g. the short name), but for anybody who is looking for the numeric day of week (as I was), you can use the new "u" format string, supported since Java 7. For example:

new SimpleDateFormat("u").format(new Date());

returns today's day-of-week index, namely: 1 = Monday, 2 = Tuesday, ..., 7 = Sunday.

Sal Borrelli
  • 2,201
  • 19
  • 19