集册 Java实例教程 在日期时间格式模式中使用可选节

在日期时间格式模式中使用可选节

欢马劈雪     最近更新时间:2020-01-02 10:19:05

388
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在日期时间格式模式中使用可选节

import java.time.LocalDate;/**来自 时 代 J a v a 公 众 号 - nowjava.com**/

import java.time.LocalDateTime;

import java.time.LocalTime;

import java.time.Month;

import java.time.format.DateTimeFormatter;


public class Main {

  public static void main(String[] args) {

    // A pattern with an optional section

    String pattern = "MM/dd/yyyy[ 'at' HH:mm:ss]";

    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);


    LocalDate ld = LocalDate.of(2012, Month.MAY, 30);

    LocalTime lt = LocalTime.of(17, 30, 12);

    LocalDateTime ldt = LocalDateTime.of(ld,lt);


    // Format a date. Optional section will be skipped because a 

    // date does not have time (HH, mm, and ss) information.

    String str1 = fmt.format(ld);

    System.out.println(str1);

展开阅读全文