2017-05-07 18 views
1

我有一个DateTime对象,我想用给定的格式打印它,比方说yyyy-MM-ddScala:以特定格式打印DateTime对象

我已经试过

val date = DateTime.now() 
val format = "yyyy-MM-dd" 
println(date.formatted(format)) 

,但得到yyyy-MM-dd仿佛格式无法识别。

我也试过

val formatter = new SimpleDateFormat(format) 
println(

,但得到

无法格式化给定的对象作为一个日期 java.lang.IllegalArgumentException异常:无法格式化给定的对象作为一个日期

如何以我选择的格式打印DateTime对象?

+0

是一个乔达日期时间? – pedrorijo91

+0

如果是这样,看看:http://stackoverflow.com/questions/20331163/how-to-format-joda-time-datetime-to-only-mm-dd-yyyy – pedrorijo91

+1

可能重复的[如何格式乔达 - 时间日期时间仅为mm/dd/yyyy?](http://stackoverflow.com/questions/20331163/how-to-format-joda-time-datetime-to-only-mm-dd-yyyy) – pedrorijo91

回答

4

日期对象有一个toString(format: String)方法。

date.toString(format) 

2017年5月7日