本文整理了Java中org.threeten.bp.format.DateTimeFormatter
类的一些代码示例,展示了DateTimeFormatter
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DateTimeFormatter
类的具体详情如下:
包路径:org.threeten.bp.format.DateTimeFormatter
类名称:DateTimeFormatter
[英]Formatter for printing and parsing date-time objects.
This class provides the main application entry point for printing and parsing. Common instances of DateTimeFormatter are provided:
For more complex formatters, a DateTimeFormatterBuilder is provided.
In most cases, it is not necessary to use this class directly when formatting. The main date-time classes provide two methods - one for formatting, format(DateTimeFormatter formatter), and one for parsing, For example:
String text = date.format(formatter);
LocalDate date = LocalDate.parse(text, formatter);
Some aspects of printing and parsing are dependent on the locale. The locale can be changed using the #withLocale(Locale) method which returns a new formatter in the requested locale.
Some applications may need to use the older Format class for formatting. The #toFormat() method returns an implementation of the old API.
This class is immutable and thread-safe.
[中]用于打印和分析日期时间对象的格式化程序。
此类提供用于打印和解析的主要应用程序入口点。提供了DateTimeFormatter的常见实例:
*使用模式字母,如yyyy MMM dd
*使用本地化样式,如长或中
*使用预定义的常量,如#ISO#u LOCAL_DATE
对于更复杂的格式化程序,提供了DateTimeFormatterBuilder。
在大多数情况下,格式化时不必直接使用此类。主要的日期时间类提供两种方法—一种用于格式化、格式化(DateTimeFormatter formatter),另一种用于解析,例如:
String text = date.format(formatter);
LocalDate date = LocalDate.parse(text, formatter);
打印和解析的某些方面取决于语言环境。可以使用#withLocale(locale)方法更改区域设置,该方法在请求的区域设置中返回一个新的格式化程序。
某些应用程序可能需要使用旧的Format类进行格式化。#toFormat()方法返回旧API的实现。
####实施者规范
这个类是不可变的,并且是线程安全的。
代码示例来源:origin: JakeWharton/u2020
@Override public String toString() {
// Returning null here is not ideal, but it lets retrofit drop the query param altogether.
return createdSince == null ? null : "created:>=" + ISO_LOCAL_DATE.format(createdSince);
}
代码示例来源:origin: googleapis/google-cloud-java
private static void checkFormat(Object value, DateTimeFormatter formatter) {
try {
formatter.parse((String) value);
} catch (DateTimeParseException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
代码示例来源:origin: prolificinteractive/material-calendarview
/**
* Format using {@link TitleFormatter#DEFAULT_FORMAT} for formatting.
*/
public DateFormatTitleFormatter() {
this(DateTimeFormatter.ofPattern(DEFAULT_FORMAT));
}
代码示例来源:origin: googleapis/google-cloud-java
Change toPb() {
Change pb = new Change();
// set id
if (getGeneratedId() != null) {
pb.setId(getGeneratedId());
}
// set timestamp
if (getStartTimeMillis() != null) {
pb.setStartTime(
DateTimeFormatter.ISO_DATE_TIME
.withZone(ZoneOffset.UTC)
.format(Instant.ofEpochMilli(getStartTimeMillis())));
}
// set status
if (status() != null) {
pb.setStatus(status().name().toLowerCase());
}
// set a list of additions
pb.setAdditions(Lists.transform(getAdditions(), RecordSet.TO_PB_FUNCTION));
// set a list of deletions
pb.setDeletions(Lists.transform(getDeletions(), RecordSet.TO_PB_FUNCTION));
return pb;
}
代码示例来源:origin: shrikanth7698/Collapsible-Calendar-View-Android
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MMMM YYYY");
String formattedDate = dateFormat.format(mAdapter.getCalendar());
mTxtTitle.setText(formattedDate);
mTableHead.removeAllViews();
代码示例来源:origin: ThreeTen/threetenbp
@Override
public Object parseObject(String text) throws ParseException {
Jdk8Methods.requireNonNull(text, "text");
try {
if (query == null) {
return formatter.parseToBuilder(text, null)
.resolve(formatter.getResolverStyle(), formatter.getResolverFields());
}
return formatter.parse(text, query);
} catch (DateTimeParseException ex) {
throw new ParseException(ex.getMessage(), ex.getErrorIndex());
} catch (RuntimeException ex) {
throw (ParseException) new ParseException(ex.getMessage(), 0).initCause(ex);
}
}
@Override
代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
{
JsonFormat.Value format = findFormatOverrides(ctxt, property, handledType());
if (format != null) {
if (format.hasPattern()) {
final String pattern = format.getPattern();
final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale();
DateTimeFormatter df;
if (locale == null) {
df = DateTimeFormatter.ofPattern(pattern);
} else {
df = DateTimeFormatter.ofPattern(pattern, locale);
}
//Issue #69: For instant serializers/deserializers we need to configure the formatter with
//a time zone picked up from JsonFormat annotation, otherwise serialization might not work
if (format.hasTimeZone()) {
df = df.withZone(DateTimeUtils.toZoneId(format.getTimeZone()));
}
return withDateFormat(df);
}
// any use for TimeZone?
}
return this;
}
}
代码示例来源:origin: googleapis/google-cloud-java
private StringBuilder toString(StringBuilder b) {
format.formatTo(LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC), b);
if (nanos != 0) {
b.append(String.format(".%09d", nanos));
}
b.append('Z');
return b;
}
代码示例来源:origin: googleapis/google-cloud-java
com.google.api.services.cloudresourcemanager.model.Project toPb() {
com.google.api.services.cloudresourcemanager.model.Project projectPb =
new com.google.api.services.cloudresourcemanager.model.Project();
projectPb.setName(name);
projectPb.setProjectId(projectId);
projectPb.setLabels(labels);
projectPb.setProjectNumber(projectNumber);
if (state != null) {
projectPb.setLifecycleState(state.toString());
}
if (createTimeMillis != null) {
projectPb.setCreateTime(
DateTimeFormatter.ISO_DATE_TIME
.withZone(ZoneOffset.UTC)
.format(Instant.ofEpochMilli(createTimeMillis)));
}
if (parent != null) {
projectPb.setParent(parent.toPb());
}
return projectPb;
}
代码示例来源:origin: org.threeten/threetenbp
@Override
public Object parseObject(String text) throws ParseException {
Jdk8Methods.requireNonNull(text, "text");
try {
if (query == null) {
return formatter.parseToBuilder(text, null)
.resolve(formatter.getResolverStyle(), formatter.getResolverFields());
}
return formatter.parse(text, query);
} catch (DateTimeParseException ex) {
throw new ParseException(ex.getMessage(), ex.getErrorIndex());
} catch (RuntimeException ex) {
throw (ParseException) new ParseException(ex.getMessage(), 0).initCause(ex);
}
}
@Override
代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp
final Locale locale = format.hasLocale() ? format.getLocale() : prov.getLocale();
if (locale == null) {
dtf = DateTimeFormatter.ofPattern(pattern);
} else {
dtf = DateTimeFormatter.ofPattern(pattern, locale);
dtf = dtf.withZone(DateTimeUtils.toZoneId(format.getTimeZone()));
代码示例来源:origin: ThreeTen/threetenbp
/**
* Formats a date-time object using this formatter.
* <p>
* This formats the date-time to a String using the rules of the formatter.
*
* @param temporal the temporal object to print, not null
* @return the printed string, not null
* @throws DateTimeException if an error occurs during formatting
*/
public String format(TemporalAccessor temporal) {
StringBuilder buf = new StringBuilder(32);
formatTo(temporal, buf);
return buf.toString();
}
代码示例来源:origin: prolificinteractive/material-calendarview
@Override
public void onDateSelected(
@NonNull MaterialCalendarView widget,
@NonNull CalendarDay date,
boolean selected) {
textView.setText(selected ? FORMATTER.format(date.getDate()) : "No Selection");
}
代码示例来源:origin: googleapis/google-cloud-java
static ChangeRequestInfo fromPb(Change pb) {
Builder builder = newBuilder();
if (pb.getId() != null) {
builder.setGeneratedId(pb.getId());
}
if (pb.getStartTime() != null) {
builder.setStartTime(
DateTimeFormatter.ISO_DATE_TIME.parse(pb.getStartTime(), Instant.FROM).toEpochMilli());
}
if (pb.getStatus() != null) {
// we are assuming that status indicated in pb is a lower case version of the enum name
builder.setStatus(ChangeRequest.Status.valueOf(pb.getStatus().toUpperCase()));
}
if (pb.getDeletions() != null) {
builder.setDeletions(Lists.transform(pb.getDeletions(), RecordSet.FROM_PB_FUNCTION));
}
if (pb.getAdditions() != null) {
builder.setAdditions(Lists.transform(pb.getAdditions(), RecordSet.FROM_PB_FUNCTION));
}
return builder.build();
}
代码示例来源:origin: googleapis/google-cloud-java
project.setCreateTime(
DateTimeFormatter.ISO_DATE_TIME
.withZone(ZoneOffset.UTC)
.format(Instant.ofEpochMilli(System.currentTimeMillis())));
if (projects.putIfAbsent(project.getProjectId(), project) != null) {
return Error.ALREADY_EXISTS.response(
代码示例来源:origin: prolificinteractive/material-calendarview
/**
* Format using a default format
*/
public DateFormatDayFormatter() {
this(DateTimeFormatter.ofPattern(DEFAULT_FORMAT, Locale.getDefault()));
}
代码示例来源:origin: org.threeten/threetenbp
/**
* Formats a date-time object using this formatter.
* <p>
* This formats the date-time to a String using the rules of the formatter.
*
* @param temporal the temporal object to print, not null
* @return the printed string, not null
* @throws DateTimeException if an error occurs during formatting
*/
public String format(TemporalAccessor temporal) {
StringBuilder buf = new StringBuilder(32);
formatTo(temporal, buf);
return buf.toString();
}
代码示例来源:origin: prolificinteractive/material-calendarview
@Override
public void onDateLongClick(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date) {
final String text = String.format("%s is available", FORMATTER.format(date.getDate()));
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
代码示例来源:origin: googleapis/google-cloud-java
static ZoneInfo fromPb(ManagedZone pb) {
Builder builder = new BuilderImpl(pb.getName());
if (pb.getDescription() != null) {
builder.setDescription(pb.getDescription());
}
if (pb.getDnsName() != null) {
builder.setDnsName(pb.getDnsName());
}
if (pb.getId() != null) {
builder.setGeneratedId(pb.getId().toString());
}
if (pb.getNameServers() != null) {
builder.setNameServers(pb.getNameServers());
}
if (pb.getNameServerSet() != null) {
builder.setNameServerSet(pb.getNameServerSet());
}
if (pb.getCreationTime() != null) {
builder.setCreationTimeMillis(
DATE_TIME_FORMATTER.parse(pb.getCreationTime(), Instant.FROM).toEpochMilli());
}
return builder.build();
}
代码示例来源:origin: com.github.joschi.jackson/jackson-datatype-threetenbp
private YearMonthDeserializer()
{
this(DateTimeFormatter.ofPattern("uuuu-MM"));
}
内容来源于网络,如有侵权,请联系作者删除!