Chr*_*ker 127
如何使用SimpleDateFormat从一种日期格式转换为另一种日期格式:
final String OLD_FORMAT = "dd/MM/yyyy";
final String NEW_FORMAT = "yyyy/MM/dd";
// August 12, 2010
String oldDateString = "12/08/2010";
String newDateString;
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d = sdf.parse(oldDateString);
sdf.applyPattern(NEW_FORMAT);
newDateString = sdf.format(d);
Run Code Online (Sandbox Code Playgroud)
Kri*_*ols 24
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
sdf.format(new Date());
Run Code Online (Sandbox Code Playgroud)
这应该可以解决问题
Bas*_*que 12
LocalDate.parse(
"23/01/2017" ,
DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK )
).format(
DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK )
)
Run Code Online (Sandbox Code Playgroud)
2017年1月23日
克里斯托弗帕克的答案是正确但过时的.麻烦的旧日期,时间类,如java.util.Date,java.util.Calendar和java.text.SimpleTextFormat现在的遗产,由取代java.time类.
将输入字符串解析为日期时间对象,然后生成所需格式的新String对象.
该LocalDate级表示没有时间一天和不同时区的日期,唯一的价值.
DateTimeFormatter fIn = DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK ); // As a habit, specify the desired/expected locale, though in this case the locale is irrelevant.
LocalDate ld = LocalDate.parse( "23/01/2017" , fIn );
Run Code Online (Sandbox Code Playgroud)
为输出定义另一个格式化程序.
DateTimeFormatter fOut = DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK );
String output = ld.format( fOut );
Run Code Online (Sandbox Code Playgroud)
2017年1月23日
顺便说一下,考虑使用标准ISO 8601格式表示日期时间值的字符串.
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
从哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.
UPDATE:该乔达时间的项目现在处于维护模式,与团队的建议迁移java.time类.这部分是为了历史而留下的.
为了好玩,这是他的代码适用于使用Joda-Time库.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
final String OLD_FORMAT = "dd/MM/yyyy";
final String NEW_FORMAT = "yyyy/MM/dd";
// August 12, 2010
String oldDateString = "12/08/2010";
String newDateString;
DateTimeFormatter formatterOld = DateTimeFormat.forPattern(OLD_FORMAT);
DateTimeFormatter formatterNew = DateTimeFormat.forPattern(NEW_FORMAT);
LocalDate localDate = formatterOld.parseLocalDate( oldDateString );
newDateString = formatterNew.print( localDate );
Run Code Online (Sandbox Code Playgroud)
转储到控制台......
System.out.println( "localDate: " + localDate );
System.out.println( "newDateString: " + newDateString );
Run Code Online (Sandbox Code Playgroud)
跑的时候......
localDate: 2010-08-12
newDateString: 2010/08/12
Run Code Online (Sandbox Code Playgroud)
String DATE_FORMAT = "yyyy/MM/dd";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
System.out.println("Formated Date " + sdf.format(date));
Run Code Online (Sandbox Code Playgroud)
完整示例:
import java.text.SimpleDateFormat;
import java.util.Date;
public class JavaSimpleDateFormatExample {
public static void main(String args[]) {
// Create Date object.
Date date = new Date();
// Specify the desired date format
String DATE_FORMAT = "yyyy/MM/dd";
// Create object of SimpleDateFormat and pass the desired date format.
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
/*
* Use format method of SimpleDateFormat class to format the date.
*/
System.out.println("Today is " + sdf.format(date));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
192187 次 |
| 最近记录: |