如何在java中将日期转换为2016年2月26日
我有2016年2月20日的日期格式,我必须将其转换为2016年2月26日。请建议我如何将其转换为这种格式如何在java中将日期转换为2016年2月26日,java,android,Java,Android,我有2016年2月20日的日期格式,我必须将其转换为2016年2月26日。请建议我如何将其转换为这种格式 import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) throws ParseException { String theInDa
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
String theInDate = "2/20/2016";
String theInFormat = "MM/dd/yyyy";
String theOutFormat = "MMM dd, yyyy";
final SimpleDateFormat theSdfInputFormatter = new SimpleDateFormat(theInFormat);
final SimpleDateFormat theSdfOutputFormatter = new SimpleDateFormat(theOutFormat);
final Date theDate = theSdfInputFormatter.parse(theInDate);
final String theDateText = theSdfOutputFormatter.format(theDate);
System.out.println(theDateText);
}
}
您可能还想检查日期解析器上的setLinent(true)功能
和新的日期API
String theInDate = "2/20/2016";
String theInFormat = "M/d/yyyy";
String theOutFormat = "MMM dd, yyyy";
final DateTimeFormatter theSdfInputFormatter = DateTimeFormatter.ofPattern( theInFormat );
final DateTimeFormatter theSdfOutputFormatter = DateTimeFormatter.ofPattern(theOutFormat);
final LocalDate theDate = LocalDate.from( theSdfInputFormatter.parse( theInDate ) );
final String theDateText = theSdfOutputFormatter.format(theDate);
System.out.println(theDateText);
注意螺纹安全。看看javadoc。
此解决方案适用于Java。对于Android来说,正如标签所示,它应该是非常相似的。
尽管这是一个重复的答案,但我希望它能帮助人们快速解决问题,并了解其工作原理,以便进一步自学。查阅文档:你能建议我必须采用哪种格式吗?这就是文档的内容,因为日期不同。20转译成2月26日的时间旅行,真的@ResearchDevelopment您的用户名很讽刺。回答明显重复的问题没有帮助。仅供参考,像、
java.text.SimpleTextFormat
这样麻烦的旧日期时间类现在被这些类取代了。看见