如何使LocalDate以特定格式返回Date? [英] How can you make LocalDate to return Date in a specific format?

查看:329
本文介绍了如何使LocalDate以特定格式返回Date?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使LocalDate以特定格式返回Date?

How can you make LocalDate to return Date in a specific format ?

Ex.本地日期ld = LocalDate.now();

Ex . LocalDate ld = LocalDate.now();

上面的语句将返回日期,如2018-11-24,但我希望它返回的日期如24-11-2018.

The above statement will return date like 2018-11-24 but i want it to return like 24-11-2018 .

**不要说使用格式化程序,因为格式化程序不会返回日期,而是会返回对我没有用的String.

** Dont say use formatter because formatter will not return date , it will return String which is of no use for me .

推荐答案

您的问题是矛盾的,例如,您想要"24-11-2018",但也想要日期"而不是字符串",而忽略了""24-11-2018"是字符串形式的日期.日期永远不会只有一种可接受的格式.因此,是的,对于将日期表示为字符串,用户应应用任何自定义的格式化程序.

Your question is contradictory, for example you want "24-11-2018" but also want "date" instead of "String" and overlook the fact that "24-11-2018" is a date in string form. A date will never have only one accepted format. So yes, for representing a date as string, users should apply any customized formatter.

但是我怀疑您想更改LocalDate-方法toString()的行为,该方法会产生ISO格式"2018-11-24"的字符串.好吧,您不能因为类LocalDate是最终类,所以不可能重写此方法,也没有配置钩子来更改行为,因为这会与类的不变性相冲突.

But I suspect that you want to change the behaviour of the LocalDate-method toString() which produces a string in the ISO-format "2018-11-24". Well, you cannot because the class LocalDate is final so overriding this method is impossible, and there is also no configuration hook to change the behaviour because this would be in conflict with the immutability of the class.

这篇关于如何使LocalDate以特定格式返回Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆