SimpleDateFormat 클래스에서 사용할 수있는 날짜 형식은 무엇입니까?
누구든지 SimpleDateFormat 클래스에서 사용 가능한 날짜 형식에 대해 알려줄 수 있습니까?
나는 API를 통과했지만 만족스러운 답변을 찾지 못했습니다. 어떤 도움이라도 대단히 감사합니다.
날짜 및 시간 형식은 아래에 잘 설명되어 있습니다.
SimpleDateFormat (Java Platform SE 7)-날짜 및 시간 패턴
n만들 수있는 형식의 수가 있을 수 있습니다 . 예- dd/MM/yyyy또는 YYYY-'W'ww-u원하는 패턴을 얻기 위해 문자를 혼합하고 일치시킬 수 있습니다. 패턴 문자는 다음과 같습니다.
G-시대 지정자 (AD)y-연도 (1996; 96)Y-주 연도 (2009, 09)M-연도 (7 월, 7 월, 07 일)w-연중 주 (27)W-월의 주 (2)D-연중 일 (189)d-일 (10)F-월의 요일 (2)E-요일 이름 (화요일, 화요일)u-요일 번호 (1 = 월요일, ..., 7 = 일요일)a-AM / PM 마커H-하루 중 시간 (0-23)k-하루 중 시간 (1-24)K-오전 / 오후 시간 (0-11)h-오전 / 오후 시간 (1-12)m-분 (30)s-초 (55)S-밀리 초 (978)z-일반 시간대 (태평양 표준시, PST, GMT-08 : 00)Z-RFC 822 시간대 (-0800)X-ISO 8601 시간대 (-08, -0800, -08 : 00)
구문 분석하려면 :
2000-01-23T04 : 56 : 07.000 + 0000
사용하다: new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
http://www3.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html 에서 얻은 몇 가지 예제 코드를 던져 보겠습니다 . 그런 다음 이해할 때까지 다양한 옵션을 사용해 볼 수 있습니다.
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) {
Date now = new Date();
//This is just Date's toString method and doesn't involve SimpleDateFormat
System.out.println("toString(): " + now); // dow mon dd hh:mm:ss zzz yyyy
//Shows "Mon Oct 08 08:17:06 EDT 2012"
SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
System.out.println("Format 1: " + dateFormatter.format(now));
// Shows "Mon, 2012-10-8 at 8:17:6 AM EDT"
dateFormatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
System.out.println("Format 2: " + dateFormatter.format(now));
// Shows "Mon 2012.10.08 at 08:17:06 AM EDT"
dateFormatter = new SimpleDateFormat("EEEE, MMMM d, yyyy");
System.out.println("Format 3: " + dateFormatter.format(now));
// Shows "Monday, October 8, 2012"
// SimpleDateFormat can be used to control the date/time display format:
// E (day of week): 3E or fewer (in text xxx), >3E (in full text)
// M (month): M (in number), MM (in number with leading zero)
// 3M: (in text xxx), >3M: (in full text full)
// h (hour): h, hh (with leading zero)
// m (minute)
// s (second)
// a (AM/PM)
// H (hour in 0 to 23)
// z (time zone)
// (there may be more listed under the API - I didn't check)
}
}
행운을 빕니다!
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 에서 형식을 확인 하십시오.
본관
System.out.println("date : " + new classname().getMyDate("2014-01-09 14:06", "dd-MMM-yyyy E hh:mm a z", "yyyy-MM-dd HH:mm"));
방법
public String getMyDate(String myDate, String returnFormat, String myFormat)
{
DateFormat dateFormat = new SimpleDateFormat(returnFormat);
Date date=null;
String returnValue="";
try {
date = new SimpleDateFormat(myFormat, Locale.ENGLISH).parse(myDate);
returnValue = dateFormat.format(date);
} catch (ParseException e) {
returnValue= myDate;
System.out.println("failed");
e.printStackTrace();
}
return returnValue;
}
java.time
최신 정보
The other Questions are outmoded. The terrible legacy classes such as SimpleDateFormat were supplanted years ago by the modern java.time classes.
Custom
For defining your own custom formatting patterns, the codes in DateTimeFormatter are similar to but not exactly the same as the codes in SimpleDateFormat. Be sure to study the documentation. And search Stack Overflow for many examples.
DateTimeFormatter f =
DateTimeFormatter.ofPattern(
"dd MMM uuuu" ,
Locale.ITALY
)
;
Standard ISO 8601
The ISO 8601 standard defines formats for many types of date-time values. These formats are designed for data-exchange, being easily parsed by machine as well as easily read by humans across cultures.
The java.time classes use ISO 8601 formats by default when generating/parsing strings. Simply call the toString & parse methods. No need to specify a formatting pattern.
Instant.now().toString()
2018-11-05T18:19:33.017554Z
For a value in UTC, the Z on the end means UTC, and is pronounced “Zulu”.
Localize
Rather than specify a formatting pattern, you can let java.time automatically localize for you. Use the DateTimeFormatter.ofLocalized… methods.
Get current moment with the wall-clock time used by the people of a particular region (a time zone).
ZoneId z = ZoneId.of( "Africa/Tunis" );
ZonedDateTime zdt = ZonedDateTime.now( z );
Generate text in standard ISO 8601 format wisely extended to append the name of the time zone in square brackets.
zdt.toString(): 2018-11-05T19:20:23.765293+01:00[Africa/Tunis]
Generate auto-localized text.
Locale locale = Locale.CANADA_FRENCH;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( locale );
String output = zdt.format( f );
output: lundi 5 novembre 2018 à 19:20:23 heure normale d’Europe centrale
Generally a better practice to auto-localize rather than fret with hard-coded formatting patterns.
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?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - 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
- Most 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 (<26), 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.
'program story' 카테고리의 다른 글
| 간단한 '평균'함수를 좌절시키는 Haskell 유형 (0) | 2020.11.11 |
|---|---|
| 쉘 스크립트 헤더 (#! / bin / sh 대 #! / bin / csh) (0) | 2020.11.11 |
| 문자 뒤의 문자열 가져 오기 (0) | 2020.11.10 |
| 하위 폴더에있는 파일을 어떻게 감시합니까? (0) | 2020.11.10 |
| Angular CLI 6 : angular.json에서 Sass 컴파일을 어떻게 추가합니까? (0) | 2020.11.10 |