[英]How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java
[英]java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy
我有一個yyyy-mm-dd
格式的java.util.Date
。 我希望它采用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);
我得到的輸出仍然不是mm-dd-yyyy
格式。
請讓我知道如何將java.util.Date
從yyyy-mm-dd
格式化為mm-dd-yyyy
Date
是自 Unix 紀元(1970 年 1 月 1 日 00:00:00 UTC)以來毫秒數的容器。
它沒有格式的概念。
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
您應該使用ThreeTen Backport
例如...
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
的目的
更新了附加示例
以防萬一第一個例子沒有意義......
此示例使用兩個格式化程序來格式化相同的日期。 然后我使用這些相同的格式化程序將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
以確保您沒有使用錯誤的模式;)
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' (Capital) 代表月 & 'm' (Simple) 代表分鍾
幾個月的一些例子
'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
LocalDate.parse(
"01-23-2017" ,
DateTimeFormatter.ofPattern( "MM-dd-uuuu" )
)
我有一個格式為 yyyy-mm-dd 的 java.util.Date
正如其他人提到的, Date
類沒有格式。 自 UTC 時間 1970 年開始以來,它有一個毫秒計數。 沒有任何附加條件。
其他答案使用麻煩的舊的遺留日期時間類,現在被 java.time 類取代。
如果您有java.util.Date
,請轉換為Instant
對象。 Instant
類表示UTC時間軸上的一個時刻,分辨率為納秒(最多九 (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 格式的字符串,YYYY-MM-DD,只需調用toString
。 java.time 類在生成/解析字符串時默認使用標准格式。
String output = localDate.toString();
2017-01-23
如果您需要 MM-DD-YYYY 格式,請定義格式模式。
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 8 及更高版本中。 這些類取代麻煩的老傳統日期時間類,如java.util.Date
, Calendar
,和SimpleDateFormat
。
現在處於維護模式的Joda-Time項目建議遷移到java.time類。
要了解更多信息,請參閱Oracle 教程。 並在 Stack Overflow 上搜索許多示例和解釋。 規范是JSR 310 。
從哪里獲得 java.time 類?
ThreeTen-Extra項目用額外的類擴展了 java.time。 該項目是未來可能添加到 java.time 的試驗場。 您可以在這里找到一些有用的類,比如Interval
, YearWeek
, YearQuarter
,和更多。
請將小“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);
您可能會得到日、月和年,並且可以將它們連接起來,或者可以使用下面給出的 MM-dd-yyyy 格式。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.