将日期设置到对象的字段中

huangapple go评论87阅读模式
英文:

Set date into object's field

问题

我有一个简单的类Message和一些字段。其中一个字段是String date。我想要设置格式化的字符串。

public class Message {
  Long id;
  String text;
  String date;
} 

我的目标是在创建Message对象时设置当前日期的格式化字符串(模式为"yyyy-MM-dd'T'HH:mm:ss.SSSXXX")。我可以通过new关键字或者使用Lombok的@Builder注解创建新对象,但是如何设置字符串date呢?

英文:

I have a simple class Message and some fields. One of them is the String date. I want to set formatted

public class Message {
  Long id;
  String text;
  String date;
} 

My goal is to set a formatted String of current date (Pattern is "yyyy-MM-dd'T'HH:mm:ss.SSSXXX") while creating object Message. I can make new object by new or by @Bulder Lombok but how can i set string date ?

答案1

得分: 1

考虑到您提供的模式,OffsetDateTime 将是我的选择:

public static void main(String[] args) {
    OffsetDateTime nowWithOffset = OffsetDateTime.now();
    System.out.println(nowWithOffset);
}

这个输出(几秒前):

2020-09-18T10:55:02.832+02:00

正如您所看到的,您期望的模式与 OffsetDateTime 使用的默认模式匹配。

我建议将属性 date 设置为 OffsetDateTime,而不是 String,并提供一个简单的方法来获取 String 表示,可能像这样:

public String getDateAsString() {
    return date.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX"));
}

如果您收到这样的 String 并想要设置您的属性,可以使用带参数的构造函数或 setter / 设置方法,例如:

public void setDateFromString(String date) {
    this.date = OffsetDateTime.parse(
                    date,
                    DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX"));
}

最好将模式中的 y 替换为 u,因为在某些需要时代的情况下,y 可能会引起问题。

英文:

Considering the pattern you provided, an OffsetDateTime would be my choice:

public static void main(String[] args) {
	OffsetDateTime nowWithOffset = OffsetDateTime.now();
	System.out.println(nowWithOffset);
}

This output (some seconds ago)

2020-09-18T10:55:02.832+02:00

As you can see, your desired pattern matches the default one used by an OffsetDateTime.

I would make the attribute date an OffsetDateTime instead of a String and provide a simple method for a String representation, maybe like this:

public String getDateAsString() {
    return date.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX");
}

and if you receive such a String and want to set your attribute, use a parameterized constructor or a setter / setting method, e.g.

public void setDateFromString(String date) {
    this.date = OffsetDateTime.parse(
                        date,
                        DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX"));
}

and better replace the y in the pattern by a u, because a y could cause trouble in some situations where an era would be required.

答案2

得分: -1

类 消息 {
    长整型 id;
    字符串 text;
    字符串 date;

    公有 消息() {
        this.date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
        系统.打印(date);
    }
}
英文:
class Message {
Long id;
String text;
String date;

public Message() {
    this.date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
    System.out.println(date);
}

}

huangapple
  • 本文由 发表于 2020年9月18日 16:37:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63952184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定