Working With Date and Time in Java

Introduction

In this article, we are going to describe most of the functions (methods) of the java.util.Calendar Class. In the following code segment, we are performing various operations on the Date object using its methods. The method getTime() of the calendar class returns a Date object that is then passed to the println() method just to print today's date. The arithmetic function of the date class adds a specific amount of time to the given time field following the calendar's rule.

About the code

In the following code segment, first we are getting the current date and time into the date object by using the getInstance() and getTime() methods of the Calendar class in a method calTime() and printing this current date and time on the console.

Then we are taking another method simpleFormatOfDate(); this method is getting an instance of the calendar class into the date object. Now we are taking an object of SimpleDateFormat class. Now we are getting the current date and time from the date object by calling thegetTime() method on this object. Now we are passing this current date and time into the format() method of the dateformatter object (SimpleDateFormat class) just to get the current date and time into the simple date format, after that we are printing current date and time on the console.

Now we are taking another method Adddates() that adds two different dates. In this method, we are taking an instance date of the calendar class into a reference variable cldr and an object dateformatter of the SimpleDateFormat class. Now we are taking the clone of the calendar class and passing the reference of this clone into cldr. Now we are getting the dates of two years ago and five years after, by using the add() method and printing these values on the console.

Now we are taking another method that is DateDifference(). In this method, we are taking the date and time from an object of the GregorianCalendar class and passing it into an object startDate1 of the Date class and also taking another object endDate1 of the date class. Now we are taking the difference of times of these two objects and printing it on the console.

Now in the last Getcalendermethods(), we are displaying values to demonstrate the various method of the Calendar class.

Program code

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.*;
public class DateAndTimeDemo {
    void calTime() {
        Date date = Calendar.getInstance().getTime();
        System.out.println("Current date and time is: " + date);
        System.out.println();
    }
    void simpleFormatOfDate() {
        Calendar date = Calendar.getInstance();
        SimpleDateFormat dateformatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
        System.out.println("Date&time in simple format: " + dateformatter.format(date.getTime()));
        System.out.println();
    }
    void Adddates() {
        System.out.println("There are some Calender operations on dates.");
        Calendar date = Calendar.getInstance();
        Calendar cldr;
        SimpleDateFormat dateformatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
        cldr = (Calendar) date.clone();
        cldr.add(Calendar.DAY_OF_YEAR, -(365 * 2));
        System.out.println("Two yr Before" + dateformatter.format(cldr.getTime()));
        cldr = (Calendar) date.clone();
        cldr.add(Calendar.DAY_OF_YEAR, +5);
        System.out.println("After five yr " + dateformatter.format(cldr.getTime()));
        System.out.println();
    }
    void DateDifference() {
        System.out.println("Difference b/w two dates");
        Date startDate1 = new GregorianCalendar(2005, 02, 25, 14, 00).getTime();
        Date endDate1 = new Date();;
        long diff = endDate1.getTime() - startDate1.getTime();
        System.out.println("  Difference b/w " + endDate1);
        System.out.println("  and " + startDate1 + " is " + (diff / (1000L * 60L * 60L * 24L)) + " days.");
        System.out.println();
    }
    void Getcalendermethods() {
        System.out.println("Following methods of the calendar class:");
        Calendar calender = Calendar.getInstance();
        System.out.println("Year : " + calender.get(Calendar.YEAR));
        System.out.println("Month  : " + calender.get(Calendar.MONTH));
        System.out.println("Day of Current Month  : " + calender.get(Calendar.DAY_OF_MONTH));
        System.out.println("Day of Current Week  : " + calender.get(Calendar.DAY_OF_WEEK));
        System.out.println("Day of Current Year  : " + calender.get(Calendar.DAY_OF_YEAR));
        System.out.println("Week of Current Year  : " + calender.get(Calendar.WEEK_OF_YEAR));
        System.out.println("Week of Month  : " + calender.get(Calendar.WEEK_OF_MONTH));
        System.out.println("Day of the Week in Month : " + calender.get(Calendar.DAY_OF_WEEK_IN_MONTH));
        System.out.println("Hour  : " + calender.get(Calendar.HOUR));
        System.out.println("AM PM : " + calender.get(Calendar.AM_PM));
        System.out.println("Hour of the Day : " + calender.get(Calendar.HOUR_OF_DAY));
        System.out.println("Minute  : " + calender.get(Calendar.MINUTE));
        System.out.println("Second  : " + calender.get(Calendar.SECOND));
        System.out.println();
    }
    public static void main(String[] args) {
        DateAndTimeDemo dt = new DateAndTimeDemo();
        System.out.println();
        dt.calTime();
        dt.simpleFormatOfDate();
        dt.Adddates();
        dt.DateDifference();
        dt.Getcalendermethods();
    }
}

Output

Administrator

Resources


Similar Articles