[英]String to date format java
I have this String date="2021-04-25T18:54:18"
and i should to format like that: HH:mm,dd mmm yyyy我有这个
String date="2021-04-25T18:54:18"
我应该这样格式化:HH:mm,dd mmm yyyy
I tried this我试过这个
String date="2021-04-25T18:54:18";
Date format= null;
try {
format = new SimpleDateFormat("HH:mm, yyyy-MM-dd'T", Locale.ENGLISH).parse(date);
holder.tvDate.setText(format.toString());
} catch (ParseException e) {
e.printStackTrace();
}
But does not work但不起作用
The legacy date-time API ( java.util
date-time types and their formatting API, SimpleDateFormat
) are outdated and error-prone.旧的日期时间 API (
java.util
日期时间类型及其格式 API, SimpleDateFormat
) 已过时且容易出错。 It is recommended to stop using them completely and switch to java.time
, the modern date-time API * .建议完全停止使用它们并切换到
java.time
, 现代日期时间 API * 。
Using modern date-time API:使用现代日期时间 API:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "2021-04-25T18:54:18";
LocalDateTime ldt = LocalDateTime.parse(strDateTime);
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("HH:mm ,dd MMM yyyy", Locale.ENGLISH);
String output = dtfOutput.format(ldt);
System.out.println(output);
}
}
Output: Output:
18:54 ,25 Apr 2021
Learn more about the modern date-time API from Trail: Date Time .从Trail: Date Time了解有关现代日期时间 API 的更多信息。
Using the legacy API:使用旧版 API:
You need two formatters: one for input pattern and one for output pattern.您需要两个格式化程序:一个用于输入模式,一个用于 output 模式。 You didn't need two formatters in the case of the modern API because the modern API is based on ISO 8601 and your date-time string is already in this format.
在现代 API 的情况下,您不需要两个格式化程序,因为现代 API 基于ISO 8601 ,并且您的日期时间字符串已经采用这种格式。
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
String strDateTime = "2021-04-25T18:54:18";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
Date date = sdfInput.parse(strDateTime);
SimpleDateFormat sdfOutput = new SimpleDateFormat("HH:mm ,dd MMM yyyy", Locale.ENGLISH);
String output = sdfOutput.format(date);
System.out.println(output);
}
}
Output: Output:
18:54 ,25 Apr 2021
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project . * For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API级别仍然不符合 Java-8,请检查Java 8+ API 可通过脱糖和如何在 Android 项目中使用 ThreeTenABP 。
You are missing 1 step.您缺少 1 个步骤。
SimpleDateFormat
can only parse dates in the format you specify. SimpleDateFormat
只能以您指定的格式解析日期。 You are trying to parse a "yyyy-MM-dd..." based string into the "HH:mm..." date.您正在尝试将基于“yyyy-MM-dd...”的字符串解析为“HH:mm...”日期。 This will not work.
这行不通。
First convert your "yyyy-MM-dd" date string into a Date.首先将您的“yyyy-MM-dd”日期字符串转换为日期。 Then, format that Date into the String you need
然后,将该日期格式化为您需要的字符串
String input = "2021-04-25T18:54:18";
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH).parse(input);
String output = new SimpleDateFormat("HH:mm, yyyy-MM-dd", Locale.ENGLISH).format(date);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.