博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java LocalDate类| 带示例的format()方法
阅读量:2531 次
发布时间:2019-05-11

本文共 2439 字,大约阅读时间需要 8 分钟。

LocalDate类format()方法 (LocalDate Class format() method)

  • format() method is available in java.time package.

    format()方法在java.time包中可用。

  • format() method is used to format this LocalDate object by using the given DateTimeFormatter object.

    format()方法用于通过使用给定的DateTimeFormatter对象来格式化此LocalDate对象。

  • format() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    format()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • format() method may throw an exception at the time of formatting this object.

    在格式化此对象时, format()方法可能会引发异常。

    DateTimeException: This exception may throw when getting any error during formatting.

    DateTimeException :格式化期间发生任何错误时,可能会引发此异常。

Syntax:

句法:

public String format(DateTimeFormatter dt_formatter);

Parameter(s):

参数:

  • DateTimeFormatter dt_formatter – represents the formatter to format this object.

    DateTimeFormatter dt_formatter –表示用于格式化此对象的格式化程序。

Return value:

返回值:

The return type of this method is String, it returns date formatted by the given formatter of this LocalDate object.

此方法的返回类型为String ,它返回由此LocalDate对象的给定格式化程序格式化的日期。

Example:

例:

// Java program to demonstrate the example // of format(DateTimeFormatter dt_formatter)// method of LocalDateimport java.time.*;import java.time.format.*;public class FormatOfLocalDate {
public static void main(String args[]) {
// Instantiates two LocalDate LocalDate l_da1 = LocalDate.parse("2007-04-04"); LocalDate l_da2 = LocalDate.now(); // Display l_da1,l_da2 System.out.println("LocalDate l_da1 and l_da2: "); System.out.println("l_da1: " + l_da1); System.out.println("l_da2: " + l_da2); System.out.println(); // Here, this method formats this date // by using the pattern "dd-MMM-yyyy" DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd-MMM-yyyy"); String format = l_da1.format(dtf); // Display format System.out.println("l_da1.format(dtf): " + format); // Here, this method formats this date by // using the pattern "dd/MM/yyyy" dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy"); format = l_da2.format(dtf); // Display format System.out.println("l_da2.format(dtf): " + format); }}

Output

输出量

LocalDate l_da1 and l_da2: l_da1: 2007-04-04l_da2: 2020-05-29l_da1.format(dtf): 04-Apr-2007l_da2.format(dtf): 29/05/2020

翻译自:

转载地址:http://bytzd.baihongyu.com/

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_02技术选型
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_汇总
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_01传统架构演进到分布式架构
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_02 微服务核心基础讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_2_04微服务下电商项目基础模块设计...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>