本文介绍了在Java中将一些特定的日期格式化程序定义为枚举的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要跟踪一些格式(对象)以在我的应用中重复使用。



➥如何通过使用Java枚举来表示这些格式化程序对象?

解决方案

tl; dr



是。

  FormatDateOnly.DD_MM_YYYY .getFormatter()



详细信息



是,您可以轻松存储一些枚举中的对象。



光滑的非常强大且灵活。



基本上,Java中的枚举几乎是一个普通的Java类。您的枚举可以具有成员变量来内部存储对象。您的枚举可以有一个构造函数,并且可以将参数传递给那些构造函数。您可以在枚举上定义方法。



让我们将这3个功能放到示例类 FormatDateOnly 枚举类中。



我们在此枚举中定义了3个对象,分别为 DD_MM_YYYY YYYY_MM_DD MM_DD_YYYY 。构造每个对象时,我们分配一个 DateTimeFormatter 对象保存在此枚举中。因为 java.time 类( )是,并且被设计为,我们可以保存单个实例以供重复使用,甚至可以跨线程使用。 Java中枚举的工作方式是,当我们的类 FormatDateOnly 首次加载时,它的一个实例,我们的三个构造函数中的每一个都被调用以使实例分配给每个名称。 / p>

我们还传递了一个字符串,用作每个枚举的显示名称。我们在 toString 重写方法中看到了此用法。

 包work.basil.example; 

import java.time.format.DateTimeFormatter;

公共枚举FormatDateOnly
{
DD_MM_YYYY(DateTimeFormatter.ofPattern( dd.MM.uuuu), DD.MM.YYYY),
YYYY_MM_DD( DateTimeFormatter.ofPattern( uuuu.MM.dd), YYYY.MM.DD),
MM_DD_YYYY(DateTimeFormatter.ofPattern( MM.dd.uuuu), MM.DD.YYYY);

私有DateTimeFormatter格式化程序;
private字符串displayName;

FormatDateOnly(DateTimeFormatter formatter,String displayName)
{
this.formatter = formatter;
this.displayName = displayName;
}

@Override
public String toString()
{
return LocalDateFormat { +
displayName =' +此.displayName +'\''+
'}';
}

public DateTimeFormatter getFormatter()
{
返回this.formatter;
}

public String getDisplayName()
{
返回this.displayName;
}
}

要使用这些枚举,我们引用所需的枚举对象,然后调用getter方法以检索存储在该枚举对象内的 DateTimeFormatter

  LocalDate localDate = LocalDate.of(2020,Month.JANUARY,23); 

字符串输出1 = localDate.format(FormatDateOnly.DD_MM_YYYY.getFormatter());
字符串输出2 = localDate.format(FormatDateOnly.MM_DD_YYYY.getFormatter());
字符串输出3 = localDate.format(FormatDateOnly.YYYY_MM_DD.getFormatter());




  • 第一部分 FormatDateOnly.DD_MM_YYYY 引用三个预先存在的对象之一(在加载类时实例化)。

  • 第二部分 .getFormatter()在该特定枚举对象上调用getter方法以检索现有的 DateTimeFormatter 对象传递给枚举的构造函数。



转储到控制台。在Java 13上运行。

  System.out.println( localDate.toString()= + localDate); 
System.out.println( output1 = + output1);
System.out.println( output2 = + output2);
System.out.println( output3 = + output3);








顺便说一句,我不一定推荐这种安排。通常,最好让 java.time 。指定表示所需的字符串长度或缩写。指定 Locale 来确定本地化所需的人类语言和文化规范。

  Locale locale = Locale.CANADA_FRENCH; 
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(locale);
字符串输出= localDate.format(formatter);




I need to track a few formats (DateTimeFormatter objects) for repeated use in my app.

➥ How to represent these formatter objects by using a Java enum?

解决方案

tl;dr

Yes.

FormatDateOnly.DD_MM_YYYY.getFormatter()

Details

Yes, you can easily store some DateTimeFormatter objects in an enum.

The slick enum facility in Java is quite powerful and flexible.

Basically, an enum in Java is almost a normal Java class. Your enum can have member variables to store objects internally. Your enum can have a constructor, and you can pass arguments to those constructors. You can define methods on your enum.

Let's put those 3 features together in this example class, FormatDateOnly enum class.

We define 3 objects in this enum, named DD_MM_YYYY, YYYY_MM_DD, and MM_DD_YYYY. When constructing each, we assign a DateTimeFormatter object to be held in this enum. Because java.time classes (tutorial) are immutable and designed to be thread-safe, we can hold a single instance for reuse even across threads. The way enums work in Java is that an instance of that when our class FormatDateOnly is first loaded, each of our three constructors is called to make an instance assigned to each name.

We also pass a string to use as the display-name of each enum. We see this used in the toString override method.

package work.basil.example;

import java.time.format.DateTimeFormatter;

public enum FormatDateOnly
{
    DD_MM_YYYY( DateTimeFormatter.ofPattern( "dd.MM.uuuu" ) , "DD.MM.YYYY" ),
    YYYY_MM_DD( DateTimeFormatter.ofPattern( "uuuu.MM.dd" ) , "YYYY.MM.DD" ),
    MM_DD_YYYY( DateTimeFormatter.ofPattern( "MM.dd.uuuu" ) , "MM.DD.YYYY" );

    private DateTimeFormatter formatter;
    private String displayName;

    FormatDateOnly ( DateTimeFormatter formatter , String displayName )
    {
        this.formatter = formatter;
        this.displayName = displayName;
    }

    @Override
    public String toString ( )
    {
        return "LocalDateFormat{" +
                "displayName='" + this.displayName + '\'' +
                '}';
    }

    public DateTimeFormatter getFormatter ( )
    {
        return this.formatter;
    }

    public String getDisplayName ( )
    {
        return this.displayName;
    }
}

To use these enums, we refer to the desired enum object, then call the getter method to retrieve the stored DateTimeFormatter stored within that enum object.

LocalDate localDate = LocalDate.of( 2020 , Month.JANUARY , 23 );

String output1 = localDate.format( FormatDateOnly.DD_MM_YYYY.getFormatter() );
String output2 = localDate.format( FormatDateOnly.MM_DD_YYYY.getFormatter() );
String output3 = localDate.format( FormatDateOnly.YYYY_MM_DD.getFormatter() );

  • The first part FormatDateOnly.DD_MM_YYYY refers to one of the three pre-existing objects (instantiated when class loaded).
  • The second part, .getFormatter() invokes the getter method on that particular enum object to retrieve the existing DateTimeFormatter object passed to the enum’s constructor.

Dump to console. Running on Java 13.

System.out.println( "localDate.toString() = " + localDate );
System.out.println( "output1 = " + output1 );
System.out.println( "output2 = " + output2 );
System.out.println( "output3 = " + output3 );


By the way, I am not necessarily recommending this arrangement. Generally it is better to let java.time automatically localize when generating text representing a date-time object. Specify a FormatStyle for how long or abbreviate you want the resulting string. Specify a Locale to determine the human language and cultural norms needed for localization.

Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( locale ) ;
String output = localDate.format( formatter ) ;

这篇关于在Java中将一些特定的日期格式化程序定义为枚举的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 04:48