Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Java Date & Time

    Java Programming Tutorials


    Java Date class is one of the Java's oldest class, and most of the methods in the class are deprecated, but we can still use it to represent a date though.

    Date and time contained in a java.util.Date instance, and accessible by using predefined methods.

    Instantiate a java.util.Date instance:

    Example:

    java.util.Date date = new java.util.Date();
    

    or

    public class DisplayDate {
       public static void main(String args[]) {
           // Instantiate an object
           Date date = new Date();
       }
    }

    Current timestamp

    Date time timestamp in milliseconds is accessible by using Date object with getTime() method.

    Example:

    import java.util.Date;
    
    public class DisplayDate {
       public static void main(String args[]) {
           // Instantiate an object
           Date date = new Date();
            
           // display current timestamp
           System.out.println(date.getTime());
       }
    }

    Program Output:

    1444194469602

    Current Date & Time

    Date time timestamp in milliseconds is accessible by using Date object with toString() method.

    Example:

    import java.util.Date;
    
    public class DisplayDate {
       public static void main(String args[]) {
           // Instantiate an object
           Date date = new Date();
            
           // display time and date
           System.out.println(date.toString());
       }
    }

    Program Output:

    Wed Oct 07 05:16:16 UTC 2015

    Comparing Dates

    Compare between two dates in Java

    Date Formatting

    SimpleDateFormat() method is used for formatting and parsing dates in a locale-sensitive manner. It's returning date-time formatting by the user-defined pattern.

    Example:

    import java.util.Date;
    import java.text.*;
    
    public class DisplayDate {
       public static void main(String args[]) {
          Date date = new Date( );
          SimpleDateFormat dateFormat = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
    
          System.out.println("Current Date: " + dateFormat.format(date)); 
           
       }
    }

    Program Output:

    Current Date: Wed 2015.10.07 at 05:48:19 AM UTC


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.