Documentation

The Java™ Tutorials
Hide TOC
Method Naming Conventions方法命名约定
Trail: Date Time
Lesson: Date-Time Overview

Method Naming Conventions方法命名约定

The Date-Time API offers a rich set of methods within a rich set of classes. 日期时间API在一组丰富的类中提供了一组丰富的方法。The method names are made consistent between classes wherever possible. 方法名称尽可能在类之间保持一致。For example, many of the classes offer a now method that captures the date or time values of the current moment that are relevant to that class. 例如,许多类提供了一个now方法,用于捕获与该类相关的当前时刻的日期或时间值。There are from methods that allow conversion from one class to another.有许多方法允许从一个类转换到另一个类。

There is also standardization regarding the method name prefixes. 还有关于方法名称前缀的标准化。Because most of the classes in the Date-Time API are immutable, the API does not include set methods. 因为日期时间API中的大多数类是不可变的,所以该API不包括set方法。(After its creation, the value of an immutable object cannot be changed. The immutable equivalent of a set method is with.) (创建后,不可变对象的值不能更改。set方法的不可变等价物是with。)The following table lists the commonly used prefixes:下表列出了常用的前缀:

Prefix前缀 Method Type方法类型 Use使用
of static factory静态工厂 Creates an instance where the factory is primarily validating the input parameters, not converting them.创建一个实例,其中工厂主要验证输入参数,而不是转换它们。
from static factory静态工厂 Converts the input parameters to an instance of the target class, which may involve losing information from the input.将输入参数转换为目标类的实例,这可能会丢失输入中的信息。
parse static factory静态工厂 Parses the input string to produce an instance of the target class.解析输入字符串以生成目标类的实例。
format instance实例 Uses the specified formatter to format the values in the temporal object to produce a string.使用指定的格式化程序格式化临时对象中的值以生成字符串。
get instance实例 Returns a part of the state of the target object.返回目标对象状态的一部分。
is instance实例 Queries the state of the target object.查询目标对象的状态。
with instance实例 Returns a copy of the target object with one element changed; this is the immutable equivalent to a set method on a JavaBean.返回目标对象的副本,其中一个元素已更改;这与JavaBean上的set方法是不可变的。
plus instance实例 Returns a copy of the target object with an amount of time added.返回添加了一定时间的目标对象的副本。
minus instance实例 Returns a copy of the target object with an amount of time subtracted.返回减去时间量的目标对象的副本。
to instance实例 Converts this object to another type.将此对象转换为其他类型。
at instance实例 Combines this object with another.将此对象与另一个对象组合。

Previous page: The Date-Time Packages
Next page: Standard Calendar