简体   繁体   English

在龙目岛漂亮打印日历

[英]pretty printing Calendar in lombok

I use lombok and it it is really great! 我用龙目岛,这真的很棒! However if I print beans that contain Calendar properties it prints the Calendar object (of course...) and it is really confusing to see the value of the Calendar. 但是,如果我打印包含Calendar属性的bean,它将打印Calendar对象(当然是...),并且看到Calendar的值确实令人困惑。 I can see something like 我可以看到类似的东西

ScheduledEvent(id=1, scheduledStartDate=java.util.GregorianCalendar[time=1492032183000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Europe/Budapest",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=151,lastRule=java.util.SimpleTimeZone[id=Europe/Budapest,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=3600000,startTimeMode=2,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=3600000,endTimeMode=2]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=3,WEEK_OF_YEAR=15,WEEK_OF_MONTH=3,DAY_OF_MONTH=12,DAY_OF_YEAR=102,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=11,HOUR_OF_DAY=23,MINUTE=23,SECOND=3,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=3600000], ...

Is it possible to assign a SimpleDateFormat to the Calendar somehow (eg if lombok meets the Calendar object then uses this formatter) and having a simple value like '2016-34-34T12:12:12.543'? 是否可以通过某种方式将SimpleDateFormat分配给Calendar(例如,如果lombok遇到Calendar对象,然后使用此格式程序),并且具有简单的值,例如“ 2016-34-34T12:12:12.543”?

Thanks, 谢谢,
V. V.

This is not specific to lombok but rather the toString() implementation of GregorianCalendar . 这不特定于lombok,而是GregorianCalendartoString()实现。

If you want to do anything about it you have several options: 如果您想对此做任何事情,则有几种选择:

  • use LocalDateTime or any of the new Time API classes which also have a more beautiful toString() output 使用LocalDateTime或任何新的Time API类,它们也具有更漂亮的toString()输出
  • wrap GregorianCalendar -instances in objects of your own and implement toString() of that wrapper class yourself GregorianCalendar -instances包装在您自己的对象中,然后自己实现该包装类的toString()
  • don't let lombok generate toString() for ScheduledEvent but rather provide your own implementation 不要让lombok为ScheduledEvent生成toString()而是提供您自己的实现
  • modify lombok to handle GregorianCalendar the way you like 修改lombok以您喜欢的方式处理GregorianCalendar

I know this post is two years ago but now there is a better soluce. 我知道这篇文章是两年前的事,但现在有了更好的解决方法。

Let lombok generate the toString() method, annotade your field with @ToString.Exclude and create a method with this annotation: @ToString.Include(name = "yourVariable") . 让lombok生成toString()方法,使用@ ToString.Exclude注释您的字段,并创建带有以下注释的方法: @ ToString.Include(name =“ yourVariable”)

Here is an example: 这是一个例子:

@Data
public class ScheduledEvent {
    private int id;

    @ToString.Exclude
    private GregorianCalendar scheduledStartDate;
    // etc ...

    @ToString.Include(name = "scheduledStartDate")
    private String formatScheduledStartDate() {
        return new SimpleDateFormat("yyyy-MM-dd").format(scheduledStartDate.getTime());
    }
}

And the toString() method returns in this case: 在这种情况下, toString()方法返回:

ScheduledEvent(id=1, scheduledStartDate=2019-03-04)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM