将格式化的字符串保存到LocalDate [英] Save a formatted String to a LocalDate

查看:176
本文介绍了将格式化的字符串保存到LocalDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DateConverter 类:

public class DateConverter extends StringConverter<LocalDate> {

    String pattern = "EEE, dd. MM. uuuu";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern,Locale.US);

    @Override
    public String toString(LocalDate object) {
        try {
            return formatter.format(object);
        } catch(Exception ex) {
            return "";
        }      
    }

    @Override
    public LocalDate fromString(String string) {
        try {
            return LocalDate.parse(string, formatter);
        } catch(Exception ex) {
            return null;
        }
    }

    public String getPattern(){
        return pattern;
    }

}

我有这段代码:

allProd.forEach((prod) -> {
    DateConverter dc = new DateConverter();
    LocalDate onMarket = dc.fromString(dc.toString(prod.getOnMarket()));
    System.out.println("localdate:" + onMarket);
    System.out.println("string:"+dc.toString(onMarket));
}

打印以下内容:

localdate:2012-11-08
string:Thu, 08. 11. 2012
localdate:2011-11-13    
string:Sun, 13. 11. 2011
localdate:2002-04-11
string:Thu, 11. 04. 2002

我想要的是我的 LocalDate 值的格式类似于字符串值。因为我有一个具有 LocalDate 字段的类,并且该字段应始终设置格式,所以我不想将字段数据类型更改为字符串。

What I want is that my LocalDate value is formatted like the string value is. Because I have a class which has a LocalDate field and the field should always be formatted. So I don't want to change the field datatype to string.

推荐答案

您不能。 code> LocalDate 没有并且不能有格式。

You cannot. A LocalDate does not have and cannot have a format in it.

如果您坚持将模型和演示文稿分开,它会将格式输入日期也将是不正确的。 LocalDate 是模型的一部分(我想)。 EEE,dd。 MM。 uuuu 格式属于您的演示文稿。您的转换器类将两者联系在一起。

If you adhere to separation of model and presentation, it would also be incorrect to put the format into the date. The LocalDate is part of your model (I presume). The EEE, dd. MM. uuuu format belongs in your presentation. Your converter class bridges the two.

A LocalDate 保留一个值,日历中的日期,仅此而已。与 int 持有值的方式几乎相同。例如, int 可以保留值64458。对于演示,您可以将其格式化为诸如000000064458,+ 64458或64,458甚至64458.00之类的字符串或十六进制格式。 int 保持不变。不管您执行哪种格式化操作,您的 LocalDate 都将保持不变。您只能在 LocalDate 之外的 String 中使用所需的格式。

A LocalDate holds a value, a date in the calendar, nothing else. Much the same way as an int holds a value. For example, an int may hold the value 64458. For presentation you may format it into strings like 000000064458, +64458 or 64,458 or even 64458.00 or in hex. The int stays the same. In the same way your LocalDate stays the same no matter which formatting operations you do. You can have your desired format only in a String outside the LocalDate.

作为一种妥协,您可以使用 getFormattedDate 方法将您的班级设置为字符串格式。您决定这是否会使模型和演示文稿之间的分隔过度模糊,或者由于应该始终设置日期格式,因此在这种情况下您可以接受。

As a compromise you may fit your class with a getFormattedDate method that formats your date into a string. You decide whether this would blur the separation between model and presentation too much, or since the date should always be formatted, in this case it’s acceptable to you.

这篇关于将格式化的字符串保存到LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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