Trong hướng dẫn này, chúng ta sẽ chuyển đổi một Chuỗi thành java.util.Date.
// String -> Date
SimpleDateFormat.parse(String);
// Date -> String
SimpleDateFormat.format(date);
Tham khảo bảng dưới đây về một số pattern ngày, giờ phổ biến trong thư viện java.text.SimpleDateFormat, tham khảo thêm tại https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
Letter | Description | Examples |
y | Year | 2023 |
M | Month in year | Jul, 07, 7 |
d | Day in month | 1 – 31 |
E | Day name in week | Monday, Friday, Sunday |
a | AM/PM marker | AM/PM |
H | Hour in day | 0 – 23 |
h | Hour in AM/PM | 1 – 12 |
m | Minute in hour | 0 – 60 |
s | Second in minute | 0 – 60 |
1. String = 7-Jul-2023
Nếu 3 chữ M, khi đó tháng được hiểu là chữ (Mon – Dec), ngược lại là số 01 – 12.
@Test
void testDate() {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String inputDate = "17-Jul-2023";
try {
Date date = formatter.parse(inputDate);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException ex) {
ex.printStackTrace();
}
}
Mon Jul 17 00:00:00 ICT 2023
17-Jul-2023
2. String = 17-07-2023
@Test
void testDate() {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
String inputDate = "17-07-2023";
try {
Date date = formatter.parse(inputDate);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException ex) {
ex.printStackTrace();
}
}
Mon Jul 17 00:00:00 ICT 2023
17-07-2023
3. String = Mon, Jul 17 2023
@Test
void testDate() {
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
String inputDate = "Mon, July 17 2023";
try {
Date date = formatter.parse(inputDate);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException ex) {
ex.printStackTrace();
}
}
Mon Jul 17 00:00:00 ICT 2023
Mon, Jul 17 2023
4. String = Monday, Jul 17, 2023 14:15:27 PM
@Test
void testDate() {
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String inputDate = "Monday, Jul 17, 2023 14:15:27 PM";
try {
Date date = formatter.parse(inputDate);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException ex) {
ex.printStackTrace();
}
}
Mon Jul 17 14:15:27 ICT 2023
Monday, Jul 17, 2023 14:15:27 PM
5. String = 2023-07-17T15:23:01Z
Hậu tố Z có nghĩa là UTC, java.util.SimpleDateFormat không phân tích cú pháp chính xác, bạn cần thay thế hậu tố Z bằng ‘+0000’.
@Test
void testDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String inputDate = "2023-07-17T15:23:01Z";
try {
Date date = formatter.parse(inputDate.replaceAll("Z$", "+0000"));
System.out.println(date);
System.out.println("time zone : " + TimeZone.getDefault().getID());
System.out.println(formatter.format(date));
} catch (ParseException ex) {
ex.printStackTrace();
}
}
Mon Jul 17 22:23:01 ICT 2023
time zone : Asia/Ho_Chi_Minh
2023-07-17T22:23:01+0700
Reference
https://mkyong.com/java/how-to-convert-string-to-date-java/
https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
awesome