development

자바로 요일을 표시하는 날짜 형식이 있습니까?

big-blog 2020. 7. 23. 07:51
반응형

자바로 요일을 표시하는 날짜 형식이 있습니까?


나는 다음과 같은 날짜 형식을 알고
"yyyy-mm-dd"형식 - 어떤 표시 날짜 2011-02-26
"yyyy-MMM-dd"표시 형식으로 날짜 - 어떤2011-FEB-26

예 :

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

요일 등을 표시하는 데 도움이되는 형식을 원합니다 2011-02-MON. 요일을 월과 연도의 문자로 표시하고 싶습니다. 이런 형식을 말해 줄 수 있습니까?


'Tue'가 표시되어야합니다.

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

'화요일'이 표시되어야합니다.

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

'T'가 표시되어야합니다.

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

구체적인 예는 다음과 같습니다.

new SimpleDateFormat("yyyy-MM-EEE").format(new Date());

-- 'E'는 트릭을 수행

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));

SimpleDateFormat sdf=new SimpleDateFormat("EEE");

EEE는 요일을 나타내며 목요일은 목요일로 표시됩니다.


"E"사용

날짜 및 시간 패턴 섹션을 참조하십시오 .

SimpleDateFormat을위한 JavaDoc


tl; dr

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

java.time

현대적인 접근법은 java.time 클래스를 사용합니다.

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

요일 이름을 번역하는 데 사용되는 인간 언어를 결정 하는 Locale등의 방법을 지정합니다 Locale.CANADA_FRENCH.

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

ISO 8601

그건 그렇고, 당신은 표준 ISO 8601 주 번호 체계에 관심이있을 수 있습니다 : yyyy-Www-d.

2018-W01-2

1 주차에는 역년의 첫 번째 목요일이 있습니다. 주 월요일에 시작됩니다. 1 년은 52 주 또는 53 주입니다. 역년의 마지막 / 처음 며칠은 다음 / 이전 주 기반 연도에 착륙 할 수 있습니다.

마지막 한 자리 숫자는 월요일-일요일의 1-7입니다.

추가 ThreeTen-추가 을위한 프로젝트에 라이브러리 클래스를 YearWeek클래스입니다.


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.


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.

참고URL : https://stackoverflow.com/questions/5121976/is-there-a-date-format-to-display-the-day-of-the-week-in-java

반응형