java.displaces를 클릭합니다.날짜 형식 yyy-mm-dd에서 mm-dd-yyy로 변환
I have a 나는 가지고 있다java.util.Date
the 형식yyyy-mm-dd
. . I want it to be in the format 포맷으로 해 주세요.mm-dd-yyyy
이 변환에 대해 시험해 본 사용 예를 다음에 나타냅니다.
// Setting the pattern
SimpleDateFormat sm = new SimpleDateFormat("mm-dd-yyyy");
// myDate is the java.util.Date in yyyy-mm-dd format
// Converting it into String using formatter
String strDate = sm.format(myDate);
//Converting the String back to java.util.Date
Date dt = sm.parse(strDate);
Still the output I am getting is not in the format 그래도 출력은 포맷이 아닙니다.mm-dd-yyyy
.
Kindly let me know how to format a a를 포맷하는 방법을 알려주세요.java.util.Date
부에서yyyy-mm-dd
to 로.mm-dd-yyyy
Date
유닉스 시대의 유닉스 시대의 컨테이너이다.Unix Epoch(1970년 1월 1일 00:00:00 UTC)는 Unix Epoch(1970년 1월 1일 00:00 UTC).
형식에 대한 개념이 없습니다.
자바 8 이상
LocalDateTime ldt = LocalDateTime.now();
System.out.println(DateTimeFormatter.ofPattern("MM-dd-yyyy", Locale.ENGLISH).format(ldt));
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH).format(ldt));
System.out.println(ldt);
출력...
05-11-2018
2018-05-11
2018-05-11T17:24:42.980
자바 7-
ThreeTen 백포트를 사용해야 합니다.
원답
예를 들면...
Date myDate = new Date();
System.out.println(myDate);
System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(myDate));
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(myDate));
System.out.println(myDate);
출력...
Wed Aug 28 16:20:39 EST 2013
08-28-2013
2013-08-28
Wed Aug 28 16:20:39 EST 2013
.Date
이 치치의 입니다. 이치DateFormatter
s
추가 예시로 업데이트됨
첫 번째 예가 말이 안 될 때를 대비해서...
이 예에서는 두 개의 포맷터를 사용하여 동일한 날짜를 포맷합니다. 이 하여 '다 되다'를 합니다.String
으로 Date
s.의 되지 않습니다.Date
가치를 보고합니다.
Date#toString
바꿀 수는 형식을 바꿀 는 있어요.Date
try {
Date myDate = new Date();
System.out.println(myDate);
SimpleDateFormat mdyFormat = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat dmyFormat = new SimpleDateFormat("yyyy-MM-dd");
// Format the date to Strings
String mdy = mdyFormat.format(myDate);
String dmy = dmyFormat.format(myDate);
// Results...
System.out.println(mdy);
System.out.println(dmy);
// Parse the Strings back to dates
// Note, the formats don't "stick" with the Date value
System.out.println(mdyFormat.parse(mdy));
System.out.println(dmyFormat.parse(dmy));
} catch (ParseException exp) {
exp.printStackTrace();
}
어떤 출력...
Wed Aug 28 16:24:54 EST 2013
08-28-2013
2013-08-28
Wed Aug 28 00:00:00 EST 2013
Wed Aug 28 00:00:00 EST 2013
또, 형식 패턴에 주의해 주세요.잘못된 패턴을 사용하고 있지 않은지 자세히 확인해 주세요.
SimpleDateFormat("MM-dd-yyyy");
대신
SimpleDateFormat("mm-dd-yyyy");
MM points Month
,mm points minutes
SimpleDateFormat sm = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sm.format(myDate);
'M'(대문자)은 월, 'm'(단순)은 분
몇 개월 동안의 예
'M' -> 7 (without prefix 0 if it is single digit)
'M' -> 12
'MM' -> 07 (with prefix 0 if it is single digit)
'MM' -> 12
'MMM' -> Jul (display with 3 character)
'MMMM' -> December (display with full name)
몇 분간의 예
'm' -> 3 (without prefix 0 if it is single digit)
'm' -> 19
'mm' -> 03 (with prefix 0 if it is single digit)
'mm' -> 19
dr;dr
LocalDate.parse(
"01-23-2017" ,
DateTimeFormatter.ofPattern( "MM-dd-uuuu" )
)
세부 사항
java.util이 있습니다.yyy-mm-dd 형식의 날짜
설명한 바와 같이, 「 」는, 「 」입니다.Date
클래스에 형식이 없습니다..770 UTC 됩니다.건은없없 없없없다다
java.time
다른 응답은 문제가 되는 오래된 날짜-시간 클래스를 사용합니다.이 클래스는 현재 java.time 클래스로 대체되었습니다.
「 」가 java.util.Date
로합니다.Instant
물건.클래스는 타임라인상의 순간을 UTC로 나노초(10진수의 최대 9자리)의 분해능으로 나타냅니다.
Instant instant = myUtilDate.toInstant();
시간대
다른 답변은 시간대의 중요한 문제를 무시합니다.날짜를 결정하려면 표준 시간대가 필요합니다.날짜는 지역에 따라 전 세계적으로 다릅니다.프랑스 파리에서는 자정이 몇 분 지난 후가 새로운 날이고, 몽트레알 퀘벡에서는 여전히 "어제"이다.
합니다.Instant
.
ZoneId z = ZoneId.of( "America/Montreal" );
를 적용합니다.ZoneId
을 얻다ZonedDateTime
.
ZonedDateTime zdt = instant.atZone( z );
LocalDate
시각 없이 날짜에만 관심이 있는 경우,LocalDate
.
LocalDate localDate = zdt.toLocalDate();
표준 ISO 8601 형식인 YYY-MM-DD로 문자열을 생성하려면 다음 명령을 클릭하십시오.toString
java.time 클래스는 문자열을 생성/파싱할 때 기본적으로 표준 형식을 사용합니다.
String output = localDate.toString();
2017-01-23
MM-DD-YYY 형식을 사용하는 경우 형식 패턴을 정의합니다.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "MM-dd-uuuu" );
String output = localDate.format( f );
포맷 패턴 코드는 대소문자를 구분합니다.질문의 코드가 잘못 사용되었습니다.mm
(분 단위)가 아니라MM
(연도의 월).
같은 것을 사용하다DateTimeFormatter
해석 대상 객체.java.time 클래스는 스레드 안전하므로 이 오브젝트를 주변에 두고 스레드 전체에서 반복적으로 재사용할 수 있습니다.
LocalDate localDate = LocalDate.parse( "01-23-2017" , f );
java.time 정보
java.time 프레임워크는 Java 8 이후에 포함되어 있습니다.이러한 클래스는 , 및 등 문제가 많은 오래된 레거시 날짜 시간 클래스를 대체합니다.
현재 유지보수 모드에 있는 Joda-Time 프로젝트는 java.time 클래스로의 이행을 권장합니다.
자세한 내용은 Oracle 자습서를 참조하십시오.또한 Stack Overflow를 검색하여 많은 예와 설명을 확인하십시오.사양은 JSR 310입니다.
java.time 클래스는 어디서 얻을 수 있습니까?
- Java SE 8 및 SE 9 이후
- 붙박이.
- 번들 구현이 포함된 표준 Java API의 일부입니다.
- Java 9에는 몇 가지 사소한 기능과 수정 사항이 추가되어 있습니다.
- Java SE 6 및 SE 7
- java.time 기능의 대부분은 ThreeTen 백포트의 Java 6 및7로 백포트됩니다.
- 안드로이드
- 쓰리텐ABP 프로젝트는 특히 ThreeTen-Backport(상기)를 Android용으로 채택하고 있습니다.
- '쓰리텐ABP 사용방법'을 참조하십시오.
쓰리텐 엑스트라 프로젝트는 java.time을 추가 클래스로 확장합니다.이 프로젝트는 향후 java.time에 추가될 수 있는 가능성을 입증하는 기반입니다.여기에는 , , , 등 유용한 클래스가 있습니다.
작은 "mm" 달을 대문자인 "MM"으로 변경해 주세요.참고할 수 있는 샘플 코드는 다음과 같습니다.
Date myDate = new Date();
SimpleDateFormat sm = new SimpleDateFormat("MM-dd-yyyy");
String strDate = sm.format(myDate);
Date dt = sm.parse(strDate);
System.out.println(strDate);
아래 코드에서는 간단하게 사용할 수 있습니다.
final Date todayDate = new Date();
System.out.println(todayDate);
System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(todayDate));
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(todayDate));
System.out.println(todayDate);
day, month, year를 취득해, 그것들을 접속하거나, 아래와 같이 MM-dd-yyy 형식을 사용할 수 있습니다.
Date date1 = new Date();
String mmddyyyy1 = new SimpleDateFormat("MM-dd-yyyy").format(date1);
System.out.println("Formatted Date 1: " + mmddyyyy1);
Date date2 = new Date();
Calendar calendar1 = new GregorianCalendar();
calendar1.setTime(date2);
int day1 = calendar1.get(Calendar.DAY_OF_MONTH);
int month1 = calendar1.get(Calendar.MONTH) + 1; // {0 - 11}
int year1 = calendar1.get(Calendar.YEAR);
String mmddyyyy2 = ((month1<10)?"0"+month1:month1) + "-" + ((day1<10)?"0"+day1:day1) + "-" + (year1);
System.out.println("Formatted Date 2: " + mmddyyyy2);
LocalDateTime ldt1 = LocalDateTime.now();
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("MM-dd-yyyy");
String mmddyyyy3 = ldt1.format(format1);
System.out.println("Formatted Date 3: " + mmddyyyy3);
LocalDateTime ldt2 = LocalDateTime.now();
int day2 = ldt2.getDayOfMonth();
int mont2= ldt2.getMonthValue();
int year2= ldt2.getYear();
String mmddyyyy4 = ((mont2<10)?"0"+mont2:mont2) + "-" + ((day2<10)?"0"+day2:day2) + "-" + (year2);
System.out.println("Formatted Date 4: " + mmddyyyy4);
LocalDateTime ldt3 = LocalDateTime.of(2020, 6, 11, 14, 30); // int year, int month, int dayOfMonth, int hour, int minute
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("MM-dd-yyyy");
String mmddyyyy5 = ldt3.format(format2);
System.out.println("Formatted Date 5: " + mmddyyyy5);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(new Date());
int day3 = calendar2.get(Calendar.DAY_OF_MONTH); // OR Calendar.DATE
int month3= calendar2.get(Calendar.MONTH) + 1;
int year3 = calendar2.get(Calendar.YEAR);
String mmddyyyy6 = ((month3<10)?"0"+month3:month3) + "-" + ((day3<10)?"0"+day3:day3) + "-" + (year3);
System.out.println("Formatted Date 6: " + mmddyyyy6);
Date date3 = new Date();
LocalDate ld1 = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date3)); // Accepts only yyyy-MM-dd
int day4 = ld1.getDayOfMonth();
int month4= ld1.getMonthValue();
int year4 = ld1.getYear();
String mmddyyyy7 = ((month4<10)?"0"+month4:month4) + "-" + ((day4<10)?"0"+day4:day4) + "-" + (year4);
System.out.println("Formatted Date 7: " + mmddyyyy7);
Date date4 = new Date();
int day5 = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date4)).getDayOfMonth();
int month5 = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date4)).getMonthValue();
int year5 = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(date4)).getYear();
String mmddyyyy8 = ((month5<10)?"0"+month5:month5) + "-" + ((day5<10)?"0"+day5:day5) + "-" + (year5);
System.out.println("Formatted Date 8: " + mmddyyyy8);
Date date5 = new Date();
int day6 = Integer.parseInt(new SimpleDateFormat("dd").format(date5));
int month6 = Integer.parseInt(new SimpleDateFormat("MM").format(date5));
int year6 = Integer.parseInt(new SimpleDateFormat("yyyy").format(date5));
String mmddyyyy9 = ((month6<10)?"0"+month6:month6) + "-" + ((day6<10)?"0"+day6:day6) + "-" + (year6);`enter code here`
System.out.println("Formatted Date 9: " + mmddyyyy9);
이것들 좀 봐..
포맷 날짜
해석일
언급URL : https://stackoverflow.com/questions/18480633/java-util-date-format-conversion-yyyy-mm-dd-to-mm-dd-yyyy
'sourcecode' 카테고리의 다른 글
JNI는 jstring을 char*로 변환합니다. (0) | 2023.01.30 |
---|---|
mysql 설명 분석이 안되는 이유는 무엇입니까? (0) | 2023.01.30 |
psycopg2: 하나의 쿼리로 여러 행을 삽입합니다. (0) | 2023.01.30 |
PHP에서 다차원 어레이를 단순 어레이로 "평탄화"하는 방법은 무엇입니까? (0) | 2023.01.30 |
테스트 실패: Jest + Vue 3 + Vuex + Typescript: TypeError: 정의되지 않은 속성 'then'을 읽을 수 없습니다. (0) | 2023.01.30 |