是否可以在Android中以dd-mm-yyyy格式获取Date对象?

时间:2018-12-02 22:17:20

标签: java android

在Android中,当您使用"Mon Dec 03 00:13:21 GMT+05:30 2018"进行转换时,所有Date对象都以这种日期格式SimpleDateFormat给出。

是否有可能在Android中以"dd-mm-yyyy"格式获取Date对象?

2 个答案:

答案 0 :(得分:1)

您可以在SimpleDateFormat上设置模式并设置日期格式:

new SimpleDateFormat("dd-mm-yyyy").format(new Date());

答案 1 :(得分:1)

您可以将Strings转换为Dates,如下所示:

String pattern = "dd-mm-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

String strDate = simpleDateFormat.format(new Date());//firstly get formatted str
Date newDate = simpleDateFormat.parse(strDate);//then get date object with wanted format