Is there a date format to display the day of the week in java?

后端 未结 6 1490
执笔经年
执笔经年 2020-12-02 07:40

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

相关标签:
6条回答
  • 2020-12-02 08:15

    Use "E"

    See the section on Date and Time Patterns:

    JavaDocs for SimpleDateFormat

    0 讨论(0)
  • 2020-12-02 08:15

    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.

    0 讨论(0)
  • 2020-12-02 08:19

    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?

    • Java SE 8, Java SE 9, and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and Java SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • Later versions of Android bundle implementations of the java.time classes.
      • For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

    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.

    0 讨论(0)
  • 2020-12-02 08:22

    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());
    
    0 讨论(0)
  • 2020-12-02 08:27
    SimpleDateFormat sdf=new SimpleDateFormat("EEE");
    

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

    0 讨论(0)
  • 2020-12-02 08:32

    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));
    
    0 讨论(0)
提交回复
热议问题