zoukankan      html  css  js  c++  java
  • How to format Date and Time in Java

    In this article, you’ll learn how to format Date and Time represented using Date, LocalDate, LocalDateTime, or ZonedDateTime to a readable String in Java.

    Format LocalDate using DateTimeFormatter

    package com.callicoder;
    
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    
    public class LocalDateFormatExample {
        
        public static void main(String[] args) {
            
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    
            LocalDate localDate = LocalDate.of(2020, 06, 15);
    
            System.out.println(localDate.format(dateTimeFormatter));
        }
    }
    

    Output

    15/06/2020
    

    Format LocalDateTime using DateTimeFormatter

    package com.callicoder;
    
    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    
    public class LocalDateTimeFormatExample {
        
        public static void main(String[] args) {
            
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy, hh:mm:ss a");
    
            LocalDateTime localDateTime = LocalDateTime.of(2020, 1, 31, 10, 45, 30);
    
            System.out.println(localDateTime.format(dateTimeFormatter));
    
        }
    }
    

    Output

    星期一, 六月 15 2020, 02:26:36 下午
    

    Format ZonedDateTime using DateTimeFormatter

    package com.callicoder;
    
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    
    public class ZonedDateTimeFormatExample {
        
        public static void main(String[] args) {
            
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy, hh:mm:ss a (VV)");
            
            ZonedDateTime zonedDateTime = ZonedDateTime.of(
                LocalDateTime.of(2020, 06, 16, 09, 52, 46),
                ZoneId.of("America/New_York"));
            
            System.out.println(zonedDateTime.format(dateTimeFormatter));
        }
    }
    

    Output

    星期二, 六月 16 2020, 10:04:46 上午 (America/New_York)
    

    Format Date and Time using SimpleDateFormat

    package com.callicoder;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateFormatExample {
        
        public static void main(String[] args) {
            
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            
            Date date = new Date();
            
            System.out.println(sdf.format(date));
        }
    }
    

    Output

    16/06/2020
    

    Let’s see another example with a more complex pattern:

    package com.callicoder;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class DateFormatExample {
        
        public static void main(String[] args) {
            
            SimpleDateFormat sdf = new SimpleDateFormat("E, MMM dd yyyy, hh:mm:ss a");
            
            Calendar calendar = Calendar.getInstance();
            calendar.set(2020, 05, 16, 10, 16, 46);
            
            Date date = calendar.getTime();
            
            System.out.println(sdf.format(date));
        }
    }
    

    Output

    星期二, 六月 16 2020, 10:16:46 上午
    
  • 相关阅读:
    剑指Offer——构建乘积数组
    剑指Offer——把二叉树打印成多行
    剑指Offer——二叉树的下一个结点
    剑指Offer——二叉搜索树与双向链表
    剑指Offer——二叉搜索树的后序遍历序列
    LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode——Construct Binary Tree from Preorder and Inorder Traversal
    剑指Offer——重建二叉树2
    C++ STL基本容器的使用
    mysql中模糊查询的四种用法介绍
  • 原文地址:https://www.cnblogs.com/PrimerPlus/p/13139607.html
Copyright © 2011-2022 走看看