Tutorials Hut

Tutorials Hut

Java - Data Time

Java date time is very useful in declaring different date formats and doing manipulation on data and time.
Java Date class is available in java library package java.util.

Java Date Constructor

Java date class has two constructors one with arguments and another without any argument:

  1. Date( ) –This constructor does not take any input and creates date object with current system date.
  2. Date(long millisec) – This constructor takes time in milli seconds and creates date object of that time.

Example of Java Data Constructors

Below program creates date with two different constructors with argument and without argument.
import java.util.Date;

public class DateTimeExample {
public static void main(String[] args) {
     Date date = new Date();
     Date dateWithTimeInMillies = new Date(92121312);
     System.out.println("date without argument="+date);
     System.out.println("date with arguments="+dateWithTimeInMillies);
 }
}

Output:

date without argument=Tue Mar 23 10:54:33 IST 2021
date with arguments=Fri Jan 02 07:05:21 IST 1970

Methods in Java Date Class

Below are few useful methods in date class:

  1. toString() – Converts date to string object.
  2. equals() – This method belongs to Object class and inherited by Date class which is used to compare two dates hash codes.
  3. getDate() – Returns the number of milliseconds that have elapsed since 1st Jan 1970
  4. setDate() – Sets date in milliseconds.
  5. hashCode() – Retursn hash code of java object.

Java SimpleDateFormat

Java has SimpleDateFormat class which is used to format date into desired or supported formats, for example if some want to format date into YYYY-mm-dd or YY-mm-dd or so one, with help of SimpleDateFormat class we can format date.

Syntax:

SimpleDateFormat <variable name for date formatter> = new SimpleDateFormat(<desired supported date format>);

Example:

Below example shows different java data formatters.

import java.text.SimpleDateFormat;

import java.util.Date;



public class DateFormatter {



	public static void main(String[] args) {



    	SimpleDateFormat sfd1 = new SimpleDateFormat("yyyy-MM-dd"); //this will format date in this format

    	SimpleDateFormat sfd2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    	Date dateToBeFormatted = new Date();



    	String formattedDate1 = sfd1.format(dateToBeFormatted);

    	String formattedDate2 = sfd2.format(dateToBeFormatted);



    	System.out.println("formattedDate1 = "+formattedDate1);

    	System.out.println("formattedDate2 = "+formattedDate2);

    	

	}

} 

Output:

formattedDate1 =2021-01-01
formattedDate1 =2021-01-01 12:12:12

Example of SimpleDateFormat to parse date string to Date object:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatter {
public static void main(String[] args) throws ParseException {
     SimpleDateFormat sfd1 = new SimpleDateFormat("yyyy-MM-dd");
     Date parsedDate = sfd1.parse("2021-01-01"); //parsing this input date string to object
     System.out.println("parsedDate = "+parsedDate);
 }
}

Output:

parsedDate = Fri Jan 01 00:00:00 IST 2021

GregorianCalendar Class

Java GregorianCalendar  class is used to get different date related fields for example year, month, day and etc.

This is an implementation of Calendar class in java.

Constructors of GregorianCalendar class:

  1. GregorianCalendar() – Creates object with current date.
  2. GregorianCalendar(int year, int month, int date) – Creates object with parameters year, month and date.
  3. GregorianCalendar(int year, int month, int date, int hour, int minute) – Creates object with parameters year, month, date, hour and minute.
  4. GregorianCalendar(int year, int month, int date, int hour, int minute, int second) – Creates object with parameters year, month, date, hour, minute and seconds.
  5. GregorianCalendar(Locale aLocale) – Creates object with parameters mentioned.
  6. GregorianCalendar(TimeZone zone) – Creates object with parameters mentioned.
  7. GregorianCalendar(TimeZone zone, Locale aLocale) – Creates object with parameters mentioned.

Example of GregorianCalendar:

 import java.util.Calendar;
import java.util.GregorianCalendar;

public class GregorianCalendarExample {
 public static void main(String[] args) {
     GregorianCalendar gregorianCalendar = new GregorianCalendar(); //New calendar with current system date time
     int year = gregorianCalendar.get(Calendar.YEAR);
     int month = gregorianCalendar.get(Calendar.MONTH);
     int day = gregorianCalendar.get(Calendar.DATE);
     int hour = gregorianCalendar.get(Calendar.HOUR);
     int minute = gregorianCalendar.get(Calendar.MINUTE);
     int seconds = gregorianCalendar.get(Calendar.SECOND);
     System.out.println("year="+year);
     System.out.println("month="+month);
     System.out.println("day="+day);
     System.out.println("hour="+hour);
     System.out.println("minute="+minute);
     System.out.println("seconds="+seconds);
  }
}

OUTPUT:

year=2021
month=2
day=23
hour=0
minute=49
seconds=37

References

Next Article To Read

You may be interested in our next articles:


















  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to Top