• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Structures » Print System Date and Time in Java Example
Next →
← Prev

Print System Date and Time in Java Example

By Dinesh Thakur

The Calendar class is an abstract class used to convert dates. We can use this class to convert a Date object to fields, such as YEAR, MONTH, HOUR, and so on. We can also use these fields to update a Date object.

Constructors

Calendar () Creates a calendar with the default TimeZone and Locale

Calendar (TimeZone , Locale) Creates a calendar with the given TimeZone and Locale

                     Methods

getTime()                                     Returns the date and time

setTime(Date)                            Sets the date and time

set(byte,int)                                Sets the specified field to the specified value

set(int,int,int)                            Sets the year, month, and date

clear(byte)                                   Clears the specified field

isSet(int)                                      Returns true if the specified field is set

setFirstDayOfWeek(byte)      Sets the first day of the week

getFirstDayOfWeek()               Returns the first day of the week

Calendar provides a class method, getInstance which returns a Calendar object whose time fields have been initialized with the current date and time:

1
Calendar k = Calendar.getInstance();

A Calendar object can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style. Calendar defines the range of values returned by certain fields, as well as their meaning. For example, the first month of the year has value MONTH == JANUARY for all calendars.

Calendar fields can be changed using three methods: set(), add(), and roll().

1
Set (f, value)

This method changes field f to value. In addition, it sets an internal member variable to indicate that field f has been changed. Although field f is changed immediately, the calendar’s milliseconds is not recomputed until the next call to get(), getTime(), or getTimeInMillis() is made. As a result of changing a field using set(), other fields may also change, depending on the field, the field value, and the calendar system. Calling set (Calendar.MONTH, Calendar. SEPTEMBER) sets the calendar to September 31, 1999. This is a temporary internal representation that resolves to October 1, 1999 if getTime () is then called. However, a call to set(Calendar.DAY_OF_MONTH, 30) before the call to getTime() sets the calendar to September 30,1999, since no recomputation occurs after set() itself.

In addition, unlike set(), add() forces an immediate recomputation of the calendar’s milliseconds and all fields.

Example: Consider a Gregorian calendar originally set to August 31, 1999. Calling add(Calendar.MONTH, 13) sets the calendar to September 30, 2000. It sets the MONTH field to September, since adding 13 months to August gives September of the next year. Since DAY_OF_MONTH cannot be 31 in September in a Gregorian calendar, it also sets the DAY_OF_MONTH to 30, the closest possible value.

Date Class

The Date class encapsulates date and time information and allows Date objects to be accessed in a system independent manner. Date provides methods for accessing specific date and time measurements, such as year, month, day, date, hours, minutes, and seconds, and for displaying dates in a variety of standard formats.

Since Date class is not able to handle internationalization and also cannot deal with the differences in daylight saving time in different locations. All the methods for converting time between binary and human readable form are thus handled by the Calendar, Gregorian calendar, TimeZone, SimpleTimeZone, and Locale classes.

Dates can be compared to each other by using their UTC values (the UTC value is the number of seconds since January I, 1970). There is a difference between UTC and standard astime (UT/GMT): one is based on an atomic clock and the other is based on astronomical observations.

Constructors

Date()                             Constructs a date using today’s date and time

Date (long)                    Constructs a date using a single UTC value

Methods

after(Date)                    Returns true if the date is later than the specified date

before(Date)                 Returns true if the date is earlier than the specified date

equals(Object)             Returns true if the date and the specified date are equal

getTime()                     Returns the time as a single UTC value

hashCode()                  Computes a hash code for the date

setTime(long)             Sets the time using a single UTC value

toString()                    Converts a date to text using UNIX ctime() conventions

import java.util.Date;
import java.util.Calendar;
class DateAndTime
{
    public static void main(String args[])
    {
        Date d=new Date();
        Calendar c = Calendar.getInstance();
        System.out.println("Current Date and Time is : "+d);
        c.set(Calendar.MONTH, 6);
        c.set(Calendar.DAY_OF_MONTH, 18);
        c.set(Calendar.YEAR, 2003 + 1900);
        System.out.print("The new date set is "+ c.get (Calendar.DAY_OF_MONTH) +"/");
        System.out.print(c.get(Calendar.MONTH)+"/");
        System.out.println(c.get(Calendar.YEAR) - 1900);
        System.out.print("Current time is "+ c.get (Calendar.HOUR_OF_DAY)+":");
        System.out.print(c.get(Calendar.MINUTE)+":");
        System.out.print(c.get(Calendar.SECOND));
    }
}

System Date and Time in Java

You’ll also like:

  1. Date Format Example in Java
  2. MySql Date Function in Java Servlet
  3. Print n Even Numbers in Java Example
  4. Print n Sequence Numbers in Java Example
  5. Print the table of an input number up till 10 times in Java Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW