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

    In this article, you’ll find several examples to get the current date, current time, current date & time, current date & time in a specific timezone in Java.

    Get current Date in Java

    package com.callicoder;
    
    import java.time.LocalDate;
    
    public class CurrentDateTimeExample {
        
        public static void main(String[] args) {
            
            // Currnet Date
            LocalDate currentDate = LocalDate.now();
            System.out.println("Current Date : " + currentDate);
        }
    }
    

    Output

    Current Date : 2020-06-15
    

    Get current Time in Java

    package com.callicoder;
    
    import java.time.LocalTime;
    
    public class CurrentDateTimeExample {
        
        public static void main(String[] args) {
            
            // Current Time
            LocalTime currentTime = LocalTime.now();
            System.out.println("Current Time : " + currentTime);
        }
    }
    

    Output

    Current Time : 13:30:27.447
    

    Get Current Date and Time in Java

    package com.callicoder;
    
    import java.time.LocalDateTime;
    
    public class CurrentDateTimeExample {
        
        public static void main(String[] args) {
            
            // Current Date and Time
            LocalDateTime currentDateTime = LocalDateTime.now();
            System.out.println("Current Date & Time : " + currentDateTime);
        }
    }
    

    Output

    Current Date and Time : 2020-06-15T13:40:52.050
    

    Get current Date and Time in specific Timezone in Java

    package com.callicoder;
    
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    
    public class CurrentDateTimeExample {
        public static void main(String[] args) {
            // Current Date and Time in a given Timezone
            ZonedDateTime currentNewYorkDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
            System.out.println(currentNewYorkDateTime);
        }
    }
    

    Output

    2020-06-15T01:50:00.410-04:00[America/New_York]
    
  • 相关阅读:
    #3146. 「APIO 2019」路灯
    #3145. 「APIO 2019」桥梁
    #3144. 「APIO 2019」奇怪装置
    雅礼集训2019 D7T2 Subsequence
    Luogu P3600 随机数生成器
    CF 704 D. Captain America
    Luogu P2570 [ZJOI2010]贪吃的老鼠
    Loj #2585. 「APIO2018」新家
    Access denied for user 'root'@'localhost' (using password: NO)
    MySql修改密码
  • 原文地址:https://www.cnblogs.com/PrimerPlus/p/13130504.html
Copyright © 2011-2022 走看看